How to add prefix to all node / express routes
You can use the express Router() for this.
You can use the router like you would use your express app. So for example:
router.use(() => {}); // General middleware
router.get('/route1', () => {})
router.get('/route2', () => {})
router.post('/route2', () => {})
And then attach the router to your express app using:
app.use('/prefix', router);
https://expressjs.com/en/4x/api.html#router
routes.js
module.exports = (app) => {
app.post('/route', (req, res) => {
res.status(status);
res.send(data);
});
app.get('/route', (req, res) => {
res.status(status);
res.send(data);
});
return app;
};
Server.js
const router = express.Router()
const routes = require('./routes')(router, {});
app.use('/PREFIX_HERE', routes)
REFER : https://expressjs.com/en/guide/using-middleware.html