To start a server with Node.js, we will need the http module. The http module is a core module which means that it comes with Node, so we won’t need any other dependencies. Here is the code that will accomplish this:
const http = require(‘http’);
const hostname = “127.0.0.1”;
const port = 3000;
const server = http.createServer((request, response) => {
response.statusCode = 200;
response.setHeader("Content-Type", "text/plain");
response.end("Hello World! I am a server. WOO HOO!")
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}`);
});
When you go to http://127.0.0.1:3000 or http://localhost:3000, you will see the text, “Hello World! I am a server. WOO HOO!”