Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Express.js router how to process same path with different middlewares?

I have two routes to register users, actually it’s not two routes since they have the same path but they have different middlewares. Since they have the same path, one of them will have precedence over other and other route will not work. Here’s the routes.

// Register a new user as an admin
router.post('/', [auth, authAdmin], async function(req, res, next) { ... });
// Sign in route
router.post('/', async function(req, res, next) { ... });

How can I solve this conflict between these two routes?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cannot achieve this with identical paths – as you’ve described the first will take precedence over the other.

You should define specific endpoints for both use cases, e.g.:

// Register a new user as an admin
router.post('/register', [auth, authAdmin], async function(req, res, next) { ... });
// Sign in route
router.post('/sign-in', async function(req, res, next) { ... });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading