0

I have

const router = express.Router();

router
  .route('/:id') 
  .delete(
    validate(messageValidator.deleteById),
    MessageController.deleteById,
  )
  .get(
    validate(messageValidator.getById),
    MessageController.getById,
  );

router
  .route('/link-metadata')
  .get(
    validate(messageValidator.link),
    MessageController.getLinkMetadata,
  );

I don't know how but, when I make a request to /link-metadata, req.route is as below: . enter image description here

This is giving me error, due to validation of first route requires Id to be Guid. It has been like this for over a year, now it is not working, if I put router .route('/link-metadata') ... above the /:id route, it works. Why is this happening?

gustavohenke
  • 40,997
  • 14
  • 121
  • 129
A.Blanc
  • 403
  • 1
  • 6
  • 15
  • 1
    My guess is that you should add some regex validation to your :id parameters (else it could be anything, include link-metadata for example). You can do it like this : `router.route('/:id([0-9]+)' etc` – SylvainF Mar 07 '19 at 14:16

1 Answers1

1

Because of the order.

.route('/:id') this means basically /.* so, any route will fall under this.

So, when you are hitting /link-metada, it is hitting the /:id route. Where req.param.id is "link-metada".

Place your /link-metadata above the /:id route and it will work.

Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35