DEV Community


Posted on • Updated on

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
Enter fullscreen mode Exit fullscreen mode
  • 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}/`);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Result:
Server running at:
Enter fullscreen mode Exit fullscreen mode

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?

Top comments (4)

terenson profile image

Is it possible to start something similar via https ?

miku86 profile image
miku86 • Edited

Hey @terenson ,

great question.

Like @arswaw said,
you need a certificate.

How to create an https server?
Node HTTPS Module

arswaw profile image

Yes but you have to have a certificate.

pandax54 profile image

Thank you so much for sharing this! =) Dankeschon