2

I have a router setup like below:

'use strict';

const express = require('express');
const controller = require('../../module/controllers/controller');

const router = express.Router();

router.get('/:param', controller.getEntity);
router.get('/', controller.getEntities);
router.put('/:param', controller.updateEntity);
router.post('/update/:param', controller.updateEntity);
router.get('/subpath', controller.getEntityPath);

module.exports = router;

All the above routes have a parent route: parent

When I try to call http://hostname/parent/subpath it keeps going to http://hostname/parent/. Only when I comment out the below lines, subpath becomes available:

'use strict';

const express = require('express');
const controller = require('../../module/controllers/controller');

const router = express.Router();

// router.get('/:param', controller.getEntity);
// router.get('/', controller.getEntities);
router.put('/:param', controller.updateEntity);
router.post('/update/:param', controller.updateEntity);
router.get('/subpath', controller.getEntityPath);

module.exports = router;

What am I doing wrong in the configuration?

2 Answers 2

8

You need to reverse the order of the routes :

'use strict';

const express = require('express');
const controller = require('../../module/controllers/controller');

const router = express.Router();

router.get('/', controller.getEntities);
router.post('/update/:param', controller.updateEntity);
router.get('/subpath', controller.getEntityPath);
router.get('/:param', controller.getEntity);
router.put('/:param', controller.updateEntity);
module.exports = router;

Because http://hostname/parent/subpath matches /:param first.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to put absolute routes above the relative routes.

router.post('/update/:param', controller.updateEntity);
router.get('/subpath', controller.getEntityPath);    
router.put('/:param', controller.updateEntity);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.