Using Express.Router()


This is the continuation from my previous post on using Express to build a simple API for an application

We're going to setup a middleware provided by Express to handle all our endpoints to our API.


Setup a - what? -- Middleware


Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle. The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.

Middleware functions can perform the following tasks:

- Execute any code.
- Make changes to the request and the response objects.
- End the request-response cycle.
- Call the next middleware in the stack.


What is a Route?

A route is a section of Express code that associates an HTTP verb (GET, POST, PUT, DELETE, etc.), an URL path/pattern, and a function that is called to handle that pattern.

There are several ways to create routes. We're going to use the express.Router() middleware as it allows us to group the route handlers for a particular part of a site together and access them using a common route-prefix. If we add routes for handling user accounts or other functions, we can keep them grouped separately.

Using Separate Router Modules

Imagine we are building an API where we have separate sections for users, books, games etc. So, lets build users section

Create a file named user.js and use the following code:

var express = require('express');
var router = express.Router();

//Mock Users To Mimic a Database

var users =
 id01 : {name : "Tommy", location: "Alaska" },  
 id02 : {name : "Jim" , location : "New York" }
router.get('/getOne/:id', function (req, res) {
  var id =

router.get('/getAll', function (req, res) {

module.exports = router;

To use the router module in our main app file we first require() the route module (user.js). We then call use() on the Express application to add the Router to the middleware handling path, specifying an URL path of 'users'.

In our main.js

var users = require('./users.js');
app.use('/users', users);

To access our defined routes in user.js route module will be accessible from

/users/getAll and users/getOne/<id>


Simlarly for books and games as well. After creating the other two router modules, just use the created modules after requiring the modules. Our code should look something like this inour main.js file.

var users = require('./users.js');
var books = require('./books.js');
var games = require('./games.js');

app.use('/users', users);
app.use('/books', books);
app.use('/games', games);


Now, when we run our code, we can access our endpoints in users, books and games at /users , /books , /games respectively. Our main.js file looks clean, clutter-free and very maintainable. This is how we can write usable code for developing an API in NodeJs using Express.Router().

In the next post, we will learn to build an actual API which has it's own database (MongoDB) with the help on mongoose package and run in locally.


blog comments powered by Disqus