How to use express.Router() Function

Route is an extremely important component of a website, it helps the website know where the user is going to the website, thereby responding appropriately.

However, if we put all routes in an app.js file, it’ll be difficult to repair, upgrade and maintain our code.

Imagine this file can be up to thousands of lines, just finding it is difficult enough.

So, what is the solution? That is, we will divide these routes by object. Each object has its own route file, so maintenance is much easier.

To do so, we use Router-level middleware - express.Router()

I. What is express.Router()?

The express.Router() function creates a new router object. We can use this function to create a new router object to handle requests.

Syntax:

express.Router();

II. How to use express.Router()

Here is a simple example of express.Router().

const app = express();
const router = express.Router();

router.use(function (req, res, next) {
  console.log("Middleware run");
  next();
});

router.get("/", function (req, res) {
  res.send("Hello world!");
});

III. express.Router() example

In this example, we’ll take a look at this app.

const express = require("express");
const app = express();

const {
  getPeople,
  createPerson,
  updatePerson,
  deletePerson,
} = require("../controllers/people");

app.get("/people", getPeople);

app.post("/people", createPerson);

app.put("/people/:id", updatePerson);

app.delete("/people/:id", deletePerson);

app.listen(5000, () => {
  console.log("Server is listening on port 5000....");
});

1. Create routes folder

Firstly, we create a new folder named routes in the node app folder. This will be the directory containing our route files.

|--nodeapp
|----routes

2. Create route file

Next, we create a people.js file to manage the routes related to the people object.

We then import express and create a router object.

const express = require("express");
const router = express.Router();

We also need to export the people route.

module.exports = router;

Now, we will copy the people-related routes from the app.js file to the people.js file.

After that, we will replace the app with router.

const express = require("express");
const router = express.Router();

const {
  getPeople,
  createPerson,
  createPersonPostman,
  updatePerson,
  deletePerson,
} = require("../controllers/people");

router.get("/people", getPeople);
router.post("/people", createPerson);
router.put("/people/:id", updatePerson);
router.delete("/people/:id", deletePerson);

module.exports = router;

4. Delete route parameter

We then delete /people parameter of the routes. The reason is that we already specified this as a route for the people, so we don’t need /people anymore.

const express = require("express");
const router = express.Router();

const {
  getPeople,
  createPerson,
  createPersonPostman,
  updatePerson,
  deletePerson,
} = require("../controllers/people");

router.get("/", getPeople);
router.post("/", createPerson);
router.put("/:id", updatePerson);
router.delete("/:id", deletePerson);

module.exports = router;

We can shortern our router code as:

const express = require("express");
const router = express.Router();

const {
  getPeople,
  createPerson,
  createPersonPostman,
  updatePerson,
  deletePerson,
} = require("../controllers/people");

router.route("/").get(getPeople).post(createPerson);
router.route("/:id").put(updatePerson).delete(deletePerson);

module.exports = router;

5. Update app.js

Back in our app.js, we require the people route.

// Require people route
const people = require("./routes/people");

We then use app.use() to set up the based route.

const express = require("express");
const app = express();

const people = require("./routes/people");

app.use("/people", people);

app.listen(5000, () => {
  console.log("Server is listening on port 5000....");
});

Now, please restart the server and test the functions again. It will work and the code is much easier to maintain and fix.