How to use express.Router() Function

Route is an essential 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. This function can 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 a 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. It’s because 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 shorten 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, making the code much easier to maintain and fix.