0

Is there any way to make all the routes that express accepts start with /api without having to define it explicitly?

Current:

this.app.get('/api/endpoint-A', (req, res) => {
   return res.send('A');
});
this.app.get('/api/endpoint-B', (req, res) => {
   return res.send('B');
});

Objective:

this.app.get('/endpoint-A', (req, res) => {//https:host.com/api/endpoint-A
   return res.send('A');
});
this.app.get('/endpoint-B', (req, res) => {//https:host.com/api/endpoint-B
   return res.send('B');
});
Vikram Deshmukh
  • 12,304
  • 4
  • 36
  • 38
Jorge Montejo
  • 485
  • 1
  • 6
  • 17

1 Answers1

3

Add

app.use('/api/:version/', router);

after you have created a base express router.

Eddie023
  • 106
  • 8