Build a nodeJS server without using express.

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 o…


This content originally appeared on DEV Community and was authored by Mattia Bonicelli

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);

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');

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) {
      
}

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);

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!');

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

const server = http.createServer(requestListener);

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

server.listen(8080);

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.


This content originally appeared on DEV Community and was authored by Mattia Bonicelli


Print Share Comment Cite Upload Translate Updates
APA

Mattia Bonicelli | Sciencx (2021-05-09T20:30:18+00:00) Build a nodeJS server without using express.. Retrieved from https://www.scien.cx/2021/05/09/build-a-nodejs-server-without-using-express/

MLA
" » Build a nodeJS server without using express.." Mattia Bonicelli | Sciencx - Sunday May 9, 2021, https://www.scien.cx/2021/05/09/build-a-nodejs-server-without-using-express/
HARVARD
Mattia Bonicelli | Sciencx Sunday May 9, 2021 » Build a nodeJS server without using express.., viewed ,<https://www.scien.cx/2021/05/09/build-a-nodejs-server-without-using-express/>
VANCOUVER
Mattia Bonicelli | Sciencx - » Build a nodeJS server without using express.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/09/build-a-nodejs-server-without-using-express/
CHICAGO
" » Build a nodeJS server without using express.." Mattia Bonicelli | Sciencx - Accessed . https://www.scien.cx/2021/05/09/build-a-nodejs-server-without-using-express/
IEEE
" » Build a nodeJS server without using express.." Mattia Bonicelli | Sciencx [Online]. Available: https://www.scien.cx/2021/05/09/build-a-nodejs-server-without-using-express/. [Accessed: ]
rf:citation
» Build a nodeJS server without using express. | Mattia Bonicelli | Sciencx | https://www.scien.cx/2021/05/09/build-a-nodejs-server-without-using-express/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.