About Express.js

Rahul Fernando
2 min readMar 25, 2020

--

Express.js is a Web server framework based on Node js core modules HTTP and connect component that designed specifically for single-page, multi-page and hybrid web applications. Those components know as middlewares.

You definitely have reinvented the wheel by continuously writing the same code on a similar challenge if you have written any serious applications using just the main Node.js modules, such as:

  • Parsing HTTP request bodies
  • Parsing cookies
  • Managing sessions
  • Handling errors
  • Extracting URL parameters

I’ll cover how Express.js works in this article to start with Express.js quickly and without getting too deeper into its API.

How Express works

Express is an NPM module (Node Package Manager) that is a dependency for your application. That means each project you are creating Express.js need to have source files in the local node_modules folder. So, you need to install like other npm modules by running npm install express.

Let’s say your application is in index.js file and you are going to run this file using node index.js then you need to require and configure Express in your index.js file. There are some statements your code will contain like:

  • Include other dependencies you have installed
  • Instantiation of Express object
  • Database connection
  • Define middlewares such as error handlers, static files
  • Define routes

There would be more if your application is complex.

Requiring Third-Party Dependencies

require our third-party dependencies by using require

var name = require(‘module-name’)

var express = require('express')
var mongoose = require('mongoose')
var bodyParser = require('body-parser')

Instantiations

To use express properly you have to instantiate it.

var app = express()

Database Connection

mongoose.connect(’databse_url’, (err) => {
if(!err) { console.log(’db connected’) }
)

App Settings

Place any app setting in app.set() method which takes string and key. Some of these keys are used which improve the action of Express.js. Sometimes, we want custom value on server object like ‘port’ then we can assign those custom values to app.set()

app.set('port', process.env.PORT || 3000)

We place the port number in environment variable if it is undefined 3000 will be used.

Define Middleware

Middlewares allows organizing your code and reuse in a better way. Some middleware is packaged as the third party. Maybe there are some custom middlewares as well.

app.use(bodyParser.json())

Define Routes

Routes may be plain old web sites or endpoints of the REST API. In both case we user app.VERB() VERB is the HTTP method like delete, post, put, get, options or patch.

app.get('/', (req, res) => {
res.send("Hello world")
})

Starting App

After all configurations, we can start our server using app.listen(port) method

app.listen(app.get('port'), () => {
console.log('server running')
})

Now our application is up and running by listening to the port 3000, each incoming requests will processed according to the defined middleware and routes from top to bottom. This aspects allow you to control execution flow.

--

--