Node JS with Express For Dummies

Żimuzo Obiechina
2 min readMay 1, 2020

--

NodeJS

This article will focus on giving an overview of how a server is set up in Node using Express for a total beginner.

The first time I set up a server for a web application was with Node. I was following along a tutorial and I couldn’t understand why I was doing what…Each line of code written just seemed like an entire galaxy too complex to explore. So I gave up on it.

However, that’s not what we programmers do! We are here to solve problems, and that includes our own. So I revisited my problem with Node and found out that looking at the ‘big picture’ of Node and what I’m using it to achieve in my application was a good starting point.

With setting up a server, I broke down my codebase in Node into compartments and converted each compartment into a simple step. Putting these steps together gave me a structure to follow. Subsequently, I could put more things into each compartment and build up a personal catalog for future reference.

I came up with these three compartments:

  1. Set up the Express server
  2. Specify middlewares
  3. Write server functions

Let’s start with the first compartment - setting up the server. Here’s how you set up an Express server:

  • Install Express as a dependency using npm
npm install express --save
  • Import Express into your file
const express = require('express');
  • Instantiate the express server
const server = express();
  • Specify the port that the server should listen
server.listen(port number);

Now you have a server up and running!

Generally, the Express server workflow requires channeling network requests and responses through a middleware to perform operations on them. You can have multiple middlewares performing different functions at various levels of your application.

Middlewares are beyond the scope of this article. You can learn more about using them here

To set up a middleware in Express, you require this one-liner :

server.use(middleware);

The use() function allows you to pass in any middleware functionality you require for your Express application.

After setting up the middleware, you can proceed to write your server functions to handle network requests and responses. These server functions could be RESTful APIs that handle ‘create-read-update-delete’ (CRUD) functionalities for the backend of your application.

Hope you found this article helpful. You can learn more about NodeJS in this documentation.

--

--