DEV Community

loading...

Build a nodeJS server without using express.

Mattia Bonicelli
Student at Code Chrysalis in Tokyo. Also undergraduate student in Electrical/Electronics engineering, with CS minor.
・2 min read

Ever wondered how to build a server without using express?

Well, look no further...
 
 

Introduction

 
While a very convenient framework express is not necessary to get up and running with a server built on nodeJS.

Looking at how express operates you'll realize that under the hood it utilizes the http module, and as such you can think of express as a wrapper for http.

With this in mind we shall attempt to spin up a server using the http module alone.
 
 

Getting started

 
The amount of code we need to write to achieve this is minimal.

const http = require('http');

const requestListener = function (req, res) {
  res.writeHead(200);
  res.end("I'm a server");
}

const server = http.createServer(requestListener);
server.listen(8080);
Enter fullscreen mode Exit fullscreen mode

Yep, that's all you need.

Assuming you name this file server.js all you need to do is run node server.js and go to http://localhost:8080/ and you will be greeted by this.

ksnip_20210510-045312

You may notice that the code to achieve this looks eerily similar to how you would do it with express...that's no coincidence.

This really reinforces the concept that Express really is just a functional layer built on top of the http module.
 
 

Dissecting the code

 
Let's take a closer look at what we've written.

const http = require('http');
Enter fullscreen mode Exit fullscreen mode

First we require, the http module so that we may use it, this should be already provided with most nodeJS installations so you should need to worry about installing it yourself.

const requestListener = function (req, res) {
      
}
Enter fullscreen mode Exit fullscreen mode

We then make a function called requestListener and expect it to take two arguments, a req (short for request) object and a res (short for response) object.

In this simple scenario we don't do anything with the request object.

  res.writeHead(200);
Enter fullscreen mode Exit fullscreen mode

In the response object that we send back we have a header containing the http status code 200, which marks a successful request.

  res.end('Hello, World!');
Enter fullscreen mode Exit fullscreen mode

When we end the request we also send back a text body containing our message.

const server = http.createServer(requestListener);
Enter fullscreen mode Exit fullscreen mode

After this we create a server that calls requestListener when a request is received.

server.listen(8080);
Enter fullscreen mode Exit fullscreen mode

And finally we tell our server to listen to port 8080 of our localhost for requests.
 
 

Summary

 
At the end of the day if you are just trying to build something simple, using express is not a necessity and you can achieve the same basic functionality by utilizing the http module.

If however you're building an API server or an app that has a database and some complexity to it, you'd likely benefit from the abstractions and quality of life improvements that express brings to the table.

Discussion (0)