🔧 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! 💻🔥
...
🔧 Built-in Modules in Node.js 🚀
📈 25.54 Punkte
🔧 Programmierung
🔧 Day 9: Python Built In Modules
📈 19.06 Punkte
🔧 Programmierung
🔧 5 Most Used Core Modules of Node.js
📈 17.5 Punkte
🔧 Programmierung