JavaScript Runtime Environment
Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. It allows developers to use JavaScript to write command-line tools and server-side scripts. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications.
Created by Ryan Dahl in 2009, Node.js has revolutionized web development by enabling full-stack JavaScript applications, where the same language can be used for both frontend and backend development.
// Simple HTTP Server const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World!\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
// File System Operations const fs = require('fs'); // Asynchronous file read fs.readFile('example.txt', 'utf8', (err, data) => { if (err) { console.error('Error reading file:', err); return; } console.log('File content:', data); }); // Synchronous file write try { fs.writeFileSync('output.txt', 'Hello, Node.js!'); console.log('File written successfully'); } catch (err) { console.error('Error writing file:', err); }
// Using Promises and Async/Await const fs = require('fs').promises; async function readAndProcessFile(filePath) { try { const data = await fs.readFile(filePath, 'utf8'); const processed = data.toUpperCase(); await fs.writeFile('processed.txt', processed); console.log('File processed successfully'); } catch (error) { console.error('Error:', error.message); } } readAndProcessFile('example.txt');
Node.js is primarily used for:
Major milestones in Node.js's development:
Node.js continues to evolve with a focus on performance, security, and developer experience.
Here are some excellent resources for learning Node.js:
Technologies often used with Node.js or alternative options: