NodeJS: How To Create A Simple Server Using The HTTP Module


So we installed NodeJS on our machine.

Now we want to learn how to create a simple server using the HTTP module.

Write a simple script

  • Open your terminal
  • Create a file named index.js:
touch index.js
  • Add this JavaScript code into it:
// import http module
const http = require("http");

// server configuration
const HOST = "";
const PORT = 8080;

// create the server
const server = http.createServer((req, res) => {

// make the server listen to requests
server.listen(PORT, HOST, () => {
  console.log(`Server running at: http://${HOST}:${PORT}/`);

Note: This is an extremely simple server. I would recommend you to read the docs of the HTTP module, especially how headers work and how to send them.

Run it from the terminal

  • Run it:
node index.js
  • Result:
Server running at:

Now you can click on the link and reach your created server.

Further Reading


  • Do you use the native HTTP/HTTPS module or some libraries like express? Why do you use it?