create Node.js web server

To create a basic Node.js web server, you can use the built-in http module. Here's an example of how to create a server that listens for incoming requests and responds with a "Hello World" message:

re‮ref‬ to:theitroad.com
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}/`);
});

In this example, we first import the http module and define the hostname and port where the server will listen for incoming requests.

We then create a new http.Server object using the createServer() method, which takes a callback function that will be called for each incoming request. The callback function takes two arguments: a http.IncomingMessage object representing the incoming request, and a http.ServerResponse object representing the outgoing response.

In the callback function, we set the response status code to 200 and the content type header to text/plain, and then send a "Hello World" message using the res.end() method.

Finally, we start the server listening on the specified hostname and port using the listen() method. Once the server is running, we log a message to the console indicating the server is running.

To test the server, you can open a web browser and visit http://localhost:3000, and you should see a "Hello World" message displayed in the browser.