Reputation: 51
This is my code, it is built with Node express. The next("route") does not work uniformly in 2 similar situations.
let express = require("express");
let app = express();
let router = express.Router();
router.use("/message/:id", function (req, res, next) {
console.log(typeof req.params.id);
if (req.params.id === 1 + "") {
console.log("the current id is 1");
next();
} else if (req.params.id === 2 + "") {
console.log("the current id is 2");
next("route");
} else if (req.params.id === 3 + "") {
console.log("the current id is 3");
next("router");
} else {
res.send("no id matched");
}
}, function (req, res) {
res.send("this is a scenario when id is 1");
});
router.use("/message/:id", function (req, res) {
res.send("this is a details page");
});
app.use("/", router, function (req, res) {
res.send("this is triggered by the app");
});
app.listen(8080, function () {
console.log("Please visit localhost:8080");
});
When I enter the URL address as this: "http://localhost:8080/message/2", the Node REPL console outputs: "the current id is 2", and the webpage displays: "this is a scenario when id is 1".
I am quite confused with this. According to the official docs of express, the next("route") will pass control to the next route.
So I assume it should display "this is a details page" rather than "this is a scenario when id is 1". Why?
To try to figure out more, I've also did some change on the codes:
let express = require("express");
let app = express();
let router = express.Router();
router.get("/message/:id", function (req, res, next) {
console.log(typeof req.params.id);
if (req.params.id === 1 + "") {
console.log("the current id is 1");
next();
} else if (req.params.id === 2 + "") {
console.log("the current id is 2");
next("route");
} else if (req.params.id === 3 + "") {
console.log("the current id is 3");
next("router");
} else {
res.send("no id matched");
}
}, function (req, res) {
res.send("this is a scenario when id is 1");
});
router.get("/message/:id", function (req, res) {
res.send("this is a details page");
});
app.use("/", router, function (req, res) {
res.send("this is triggered by the app");
});
app.listen(8080, function () {
console.log("Please visit localhost:8080");
});
I simply replaced the router.use with router.get, and this time when I revisit "http://localhost:8080/message/2", the webpage displays "this is a details page".
Why in both scenarios the result is different?
Upvotes: 0
Views: 1701
Reputation: 3394
router.get is only for defining subpaths
var router = express.Router();
app.use('/api', router); // Mount the router as middleware at path /api
router.get('/signup', signup);
router.get('/user', userInfo);
If you open /api/signup, then the signup function will get called.
If you open /api/user, then the userInfo function will get called.
while using router.use() you can give only middleware, like this:
router.use(function(req, res, next) {
console.log('%s %s %s', req.method, req.url, req.path);
next();
});
Upvotes: 0
Reputation: 849
It is stated that in the docs that
To skip the rest of the middleware functions from a router middleware stack, call next('route') to pass control to the next route. NOTE: next('route') will work only in middleware functions that were loaded by using the app.METHOD() or router.METHOD() functions.
Pay attention to the NOTE section, next('route') is meant to skip, but in first case it was not skipping because you did not use route.METHOD(), you used router.use()
Upvotes: 4