Simple Node.js HTTP server example responding with ’Hello World’ without frameworks, ideal for learning basic server creation on port 3000.
javascript
// Import the native Node.js http module
const http = require('http');
// Create an HTTP server
const server = http.createServer((req, res) => {
// Set status code to 200 (OK) and content-type to text/plain
res.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response 'Hello World'
res.end('Hello World');
});
// The server listens on port 3000
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});Node.js: JavaScript runtime required to execute the code.
Node.js 'http' module: native module used to create the HTTP server.
No external dependencies: no additional packages are required.
Configuration: the server listens on port 3000 (modifiable in the code).