Routing using NodeJS and Express

Sumant Mishra
4 min readJan 23, 2019

ExpressJS is one of the most popular NodeJS framework. It provides several features which makes server side programming easy. Routing is one of those several features. Routing is defining path (URI) for a resource deployed in your server and determining how application will respond to the requests from client. As per the definition mentioned in Express documentation:

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).

The basic structure to define a route using Express framework is:

app.METHOD(path, handler);

Above structure is a combination of express instance, HTTP request method, route path and handler.

Route Path

Route paths are the endpoints to a resource on the server where the request to be made. This can be a string or pattern (i.e. ‘/abc/*’, ‘/abc*/*’, ‘/ab?cd’ or ‘/ab*cd’ etc.)

Route Methods

Express supports several routing methods (appx 23 methods). But four (GET, POST, PUT and DELETE) out of those several methods are commonly used.

The GET Method

GET is one of the mostly used HTTP methods and is used to access a resource from the server or to request data from an URI. As recommended, GET request should only be used to retrieve data and there should not have any other effect. Query parameters can be sent by appending to path for the GET request but the length of the path is restricted to a certain number. GET requests can be cached and bookmarked. It is recommended not to use GET requests in case of some critical data processing.

Example of GET request is:

Above code will print ‘Hello World!’ in browser when default path of the server is loaded in the browser.

The POST Method

The POST method is used to send some to an URI for further processing, create/update a resource or store in database. The data sent to the server can be extracted from the req.body in the callback function (or middleware function). Often POST is used to create a new entity, but it can also be used to update an entity.

Example of POST request to default path or homepage is:

The PUT Method

PUT can be used to create a new entity or update an existing one. A PUT request is idempotent. That is, calling the same PUT request multiple times will always produce the same result. In contrast, calling a POST request repeatedly make have side effects of creating the same resource multiple times.

Example of PUT request to an URI:

The DELETE Method

The DELETE method is used a resource at the specified URL. Example is:

Route handlers
Single, multiple or an array of callback functions can be provided as the route handlers. If multiple or array of callback functions are used, then it is necessary to call next() method in all methods except the last one in sequence. Below are few examples of providing callback functions:

Single callback function:

More than one callback function (or middleware function):

Array of callback functions:

Request and Response (req and res) parameters

Each callback function has two basic parameters request and response. Request (req) is used to extract information related to the HTTP request like POST data, headers etc. Using Response (res) parameter, response can be sent to the client. Below are the list of few response methods:

  • res.download(): Prompts a file for downloading.
  • res.end(): End the response process.
  • res.json(): Send a JSON response.
  • res.redirect(): Redirects a request to a new path (route).
  • res.render(): Render a view template.
  • res.send(): Send a response of various types.
  • res.sendStatus(): Set the response status code and send its string representation as the response body.

Reference:

https://expressjs.com/en/guide/routing.html
https://expressjs.com/en/4x/api.html#app.get.method
https://spring.io/understanding/REST
https://www.w3schools.com/tags/ref_httpmethods.asp

--

--

Sumant Mishra

Fullstack Architect || TOGAF 9 || AWSCSAA || Cloud Practitioner || NodeJS || React & Angular || Docker || Coder