Reputation: 395
I am having trouble wrapping my head around how to use the /:variable
notation effectively. In my setup I have this layout.
router.route("/").get()
router.route("/:id").get().put().post().delete()
router.route("/auth").get().put().post()
When I call /auth
it fails to go there, instead it triggers the method under the /:id
. How can I make sure when I say /auth it does not goto that /:id
path.
Upvotes: 0
Views: 239
Reputation: 102597
You need to adjust the order of route definition. Move /auth
route to /:id
before route.
E.g.
import express, { Router } from 'express';
const app = express();
const port = 3000;
const router = Router();
router.route('/auth').get((req, res) => {
res.send('auth');
});
router.route('/:id').get((req, res) => {
res.send({ id: req.params.id });
});
app.use('/user', router);
app.listen(port, () => console.log(`HTTP server is listening on http://localhost:${port}`));
Test and output:
⚡ curl http://localhost:3000/user/auth
auth%
⚡ curl http://localhost:3000/user/123
{"id":"123"}%
Upvotes: 1