Skip to content

Create routers

Course: PayTM project - chapter 3

In the index.js file, route all the requests to /api/v1 to a apiRouter defined in backend/routes/index.js

Create a new file backend/routes/index.js that exports a new express router.

( How to create a router - https://www.geeksforgeeks.org/express-js-express-router-function/ )

Solution
backend/api/index.js
const express = require('express');
const router = express.Router();
module.exports = router;

Import the router in index.js and route all requests from /api/v1 to it

Solution
backend/index.js
const express = require("express");
const rootRouter = require("./routes/index");
const app = express();
app.use("/api/v1", rootRouter);