Lädt...

🔧 Built-in Modules in Node.js 🚀


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Hey there, awesome devs! 👋 Have you ever wondered how Node.js handles files, streams, networking, and more without installing extra packages? The secret lies in built-in modules—powerful tools that come preloaded with Node.js! 😍

📌 What Are Built-in Modules?

Built-in modules are core modules that come with Node.js, so you don’t need to install them separately. Just require or import them, and you’re good to go! 🎉

Here’s how to import built-in modules:

🔹 CommonJS (require)

const fs = require('fs');

🔹 ES Modules (import)

import fs from 'fs';

Now, let’s explore some of the most useful modules! 🚀

🔥 1. fs (File System) - Work with Files 📂

The fs module allows you to read, write, update, and delete files.

📖 Read a File

const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

✍️ Write to a File

fs.writeFile('output.txt', 'Hello, Node.js!', (err) => {
  if (err) throw err;
  console.log('File written successfully!');
});

🔥 2. path - Work with File Paths 🗂️

The path module makes handling file paths easy and OS-independent.

📌 Get File Extension

const path = require('path');
console.log(path.extname('index.html')); // Output: .html

📌 Join Paths Correctly

console.log(path.join(__dirname, 'folder', 'file.txt'));

🔥 3. http - Create a Web Server 🌐

The http module lets you build simple web servers.

🚀 Create a Basic Server

const http = require('http');
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, World!');
});
server.listen(3000, () => console.log('Server running on port 3000'));

Open http://localhost:3000/ in your browser and see it in action! 🚀

🔥 4. os - Get System Information 🖥️

The os module provides system-related info like OS type, memory, and CPU details.

🖥️ Get OS Type

const os = require('os');
console.log(os.type()); // Output: Windows_NT, Linux, Darwin

💾 Check Free Memory

console.log(os.freemem());

🔥 5. events - Work with Events 🎯

The events module allows you to create and handle custom events.

🔥 Create an Event Emitter

const EventEmitter = require('events');
const eventEmitter = new EventEmitter();

eventEmitter.on('greet', () => console.log('Hello, Developer!'));
eventEmitter.emit('greet');

🚀 Final Thoughts

Node.js built-in modules make development faster and easier by providing powerful utilities right out of the box! Whether you’re handling files, paths, servers, or system info, Node.js has you covered! 💪

Stay tuned for the next article, where we’ll explore even more Callback Pattern! 🎯

If you found this blog helpful, make sure to follow me on GitHub 👉 github.com/sovannaro and drop a ⭐. Your support keeps me motivated to create more awesome content! 🚀

Happy coding! 💻🔥

...

🔧 ES Modules in Node.js: The Modern Way to Handle Modules 🚀


📈 28.51 Punkte
🔧 Programmierung

🔧 Built-in Modules in Node.js 🚀


📈 25.54 Punkte
🔧 Programmierung

🔧 Building a RESTful API with Node.js: A Step-by-Step Guide - Part Two - Node Modules


📈 23.98 Punkte
🔧 Programmierung

🕵️ Node.js third-party modules: [node-df] RCE via insecure command concatenation


📈 23.98 Punkte
🕵️ Sicherheitslücken

🔧 Practical C++20 Modules and the future of tooling around C++ Modules with Cameron DaCamara


📈 22.03 Punkte
🔧 Programmierung

🕵️ CVE-2024-25298 | REDAXO 5.15.1 modules.modules.php information disclosure


📈 22.03 Punkte
🕵️ Sicherheitslücken

🕵️ GitHub - ZehMatt/zasm-modules: Generating binary modules with zasm


📈 22.03 Punkte
🕵️ Reverse Engineering

🐧 Node-OS: The first operating system powered by node.js and npm built on top of the linux kernel


📈 21.01 Punkte
🐧 Linux Tipps

🔧 Getting Started with Node.js: Understanding Node, npm, nvm, and npx (and How to Install Node.js)


📈 19.45 Punkte
🔧 Programmierung

🔧 Day 9: Python Built In Modules


📈 19.06 Punkte
🔧 Programmierung

🪟 PNY's latest memory modules are colorful and built for high-end gaming


📈 19.06 Punkte
🪟 Windows Tipps

🔧 Local Modules in Node.js: Organize Your Code Like a Pro 🚀


📈 17.5 Punkte
🔧 Programmierung

🕵️ Node.js third-party modules: [devcert] Command Injection via insecure command formatting


📈 17.5 Punkte
🕵️ Sicherheitslücken

🕵️ Node.js third-party modules: gitlabhook OS Command Injection


📈 17.5 Punkte
🕵️ Sicherheitslücken

🔧 Modules in Node.js: The Building Blocks of Your App 🚀


📈 17.5 Punkte
🔧 Programmierung

🕵️ Node.js third-party modules: Pixel flood attack cause the javascript heap out of memory


📈 17.5 Punkte
🕵️ Sicherheitslücken

🔧 TIL: Synchronous import in ES-Modules using module.createRequire (Node)


📈 17.5 Punkte
🔧 Programmierung

🕵️ Node.js third-party modules: [logkitty] RCE via insecure command formatting


📈 17.5 Punkte
🕵️ Sicherheitslücken

🕵️ Node.js third-party modules: [@firebase/util] Prototype pollution


📈 17.5 Punkte
🕵️ Sicherheitslücken

🔧 Launching Vratix - open source Node.js API modules


📈 17.5 Punkte
🔧 Programmierung

🕵️ Node.js third-party modules: [utils-extend] Prototype pollution


📈 17.5 Punkte
🕵️ Sicherheitslücken

🕵️ Node.js third-party modules: [ts-dot-prop] Prototype Pollution


📈 17.5 Punkte
🕵️ Sicherheitslücken

🕵️ Node.js third-party modules: XSS in Bootbox


📈 17.5 Punkte
🕵️ Sicherheitslücken

🔧 5 Most Used Core Modules of Node.js


📈 17.5 Punkte
🔧 Programmierung

🕵️ Node.js third-party modules: Server-Side Request Forgery (SSRF) in Ghost CMS


📈 17.5 Punkte
🕵️ Sicherheitslücken

🕵️ Node.js third-party modules: [serve] Path Traversal


📈 17.5 Punkte
🕵️ Sicherheitslücken

🕵️ Node.js third-party modules: [json8-merge-patch] Prototype Pollution


📈 17.5 Punkte
🕵️ Sicherheitslücken

matomo