Sitemap
Sathesh Ramasamy

Sathesh Ramasamy

Simple Node.js Server

Apr 24, 2023

here is the example of simple nodejs server

const http = require('http');

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World!');
});

const PORT = process.env.PORT || 3000;

server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});

This server listens for incoming HTTP requests on the specified port (either the PORT environment variable or port 3000 by default), and responds with a simple "Hello World!" message.

Get Sathesh Ramasamy’s stories in your inbox

Join Medium for free to get updates from this writer.

You can save this code in a file with a .js extension (e.g. server.js), and then run it with the command node server.js. Once the server is running, you can access it by navigating to http://localhost:3000 in your web browser (assuming you're running it on port 3000).

--

--