Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 9, 2021 08:30 pm GMT

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 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.


Original Link: https://dev.to/bonicellimattia/build-a-nodejs-server-without-using-express-52ed

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To