Node.js is an open-source, cross-platform JavaScript runtime environment that allows you to run JavaScript code on the server side. It was created by Ryan Dahl in 2009 and has since become widely used for building scalable network applications, especially web servers and APIs.
Node.js is designed to build scalable network applications easily. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. Node.js is built on the V8 JavaScript engine, which makes it fast and highly performant. It also has a vast ecosystem of libraries and tools available through NPM (Node Package Manager), which simplifies the development process by allowing developers to reuse code and share solutions.
Feature | Description |
---|---|
Asynchronous and Event-Driven | Non-blocking I/O model for handling multiple operations concurrently. |
Single-Threaded but Scalable | Uses a single thread with event loop for handling thousands of connections. |
NPM | Largest ecosystem of open-source libraries and packages. |
Cross-Platform | Runs on Windows, macOS, and Linux. |
Built for Networking | Ideal for real-time communication applications. |
In this example, Node.js is used to create a basic HTTP server that listens on port 3000 and responds with “Hello, World!” for any incoming request.
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}/`);
});