Run A Basic Node JS Server - Print Hello World in Node JS

Node.js is an open-source JavaScript runtime environment. Below is the basic code to print "Hello, World!" in Node.js or run a very basic server. We are using the HTTP module to create a server with the Content-Type "text/html" on port 3000. You can access it at 127.0.0.1:3000 after running the command "node your-file.js". If you haven't installed Node.js yet, you can do so from here: Install Node JS, and with the copy of the below code you can run your first Node.js Server if you haven't done this yet.

const { createServer } = require('node:http');

const hostname = '127.0.0.1';
const port = 3000;

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

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});


Advertisement
Advertisement

Post a Comment

0 Comments

© Copyright 2024 & 2025 - Team Krope - All right reserved