简体   繁体   中英

Node.js Express - Why next(“route”) does not lead to the next route?

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?

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()

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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM