Node.js 中间件next()路由处理器

Node.js 中间件next()路由处理器,node.js,express,routes,middleware,Node.js,Express,Routes,Middleware,我正在读这篇文章 在中间件部分,作者很好地解释了next()的好处 写这个 var app = require("express")(); function checkLogin() { return false; } function logRequest() { console.log("New request"); } app.get("/dashboard", function(httpRequest

我正在读这篇文章

在中间件部分,作者很好地解释了next()的好处

写这个

var app = require("express")();

function checkLogin()
{
    return false;
}

function logRequest()
{
    console.log("New request");
}

app.get("/dashboard", function(httpRequest, httpResponse, next){

    logRequest();

    if(checkLogin())
    {
        httpResponse.send("This is the dashboard page");
    }
    else
    {
        httpResponse.send("You are not logged in!!!");
    }
});

app.get("/profile", function(httpRequest, httpResponse, next){

    logRequest();

    if(checkLogin())
    {
        httpResponse.send("This is the dashboard page");
    }
    else
    {
        httpResponse.send("You are not logged in!!!");
    }
});

app.listen(8080);
使用next()可以更干净

var app = require("express")();

function checkLogin()
{
    return false;
}

function logRequest()
{
    console.log("New request");
}

app.get("/*", function(httpRequest, httpResponse, next){
    logRequest();
    next();
})

app.get("/*", function(httpRequest, httpResponse, next){

    if(checkLogin())
    {
        next();
    }
    else
    {
        httpResponse.send("You are not logged in!!!");
    }
})

app.get("/dashboard", function(httpRequest, httpResponse, next){

        httpResponse.send("This is the dashboard page");

});

app.get("/profile", function(httpRequest, httpResponse, next){

        httpResponse.send("This is the dashboard page");

});

app.listen(8080);

但我不明白的是:next()如何知道它应该在/dashboard或/profile路由处理程序中作为next运行?

只需转到“路由器句柄列表”中的下一个路由即可。顺序非常重要,在您的示例中,路由器列表如下所示
GET/*(任何东西)
->
GET/*(任何东西)
->
GET/dashboard
->
GET/profile

您的浏览器请求是
GET/profile
,请按顺序检查方法和路径:

是匹配任何东西->是的,做点什么,下一步

是匹配任何东西->是的,做点什么,下一步

是match GET/dashboard->No,不执行dashboard处理程序,检查阵列中的下一个路由器

是match GET/profile->Yes,做点什么,调用
httpResponse.send
->完成

如果您在
app.get(“/*”,
route to check
login
之前注册了一个路由,它将在不检查login的情况下通过

...
app.get("/secret", function(httpRequest, httpResponse, next){

        httpResponse.send("Tada!!!");
});

app.get("/*", function(httpRequest, httpResponse, next){

    if(checkLogin())
    {
        next();
    }
    else
    {
        httpResponse.send("You are not logged in!!!");
    }
})
...

只需转到“路由器句柄列表”中的下一条路由。顺序非常重要,在您的示例中,路由器列表如下所示
GET/*(任何东西)
->
GET/*(任何东西)
->
GET/dashboard
->
GET/profile

您的浏览器请求是
GET/profile
,请按顺序检查方法和路径:

是匹配任何东西->是的,做点什么,下一步

是匹配任何东西->是的,做点什么,下一步

是match GET/dashboard->No,不执行dashboard处理程序,检查阵列中的下一个路由器

是match GET/profile->Yes,做点什么,调用
httpResponse.send
->完成

如果您在
app.get(“/*”,
route to check
login
之前注册了一个路由,它将在不检查login的情况下通过

...
app.get("/secret", function(httpRequest, httpResponse, next){

        httpResponse.send("Tada!!!");
});

app.get("/*", function(httpRequest, httpResponse, next){

    if(checkLogin())
    {
        next();
    }
    else
    {
        httpResponse.send("You are not logged in!!!");
    }
})
...

取决于请求路径。
next()
使请求从一个中间件转到管道中的下一个中间件。下一个中间件是否执行,取决于请求路径。@Yousaf,啊,好的,我理解这就是为什么app.get(“/*”),然后当路径为profile时,它将转到profile取决于请求路径。
next()
将请求从一个中间件转到管道中的下一个中间件。下一个中间件是否执行,取决于请求路径。@Yousaf,啊,好吧,我明白这就是为什么app.get(“/*”,)在路径为profile时,它将转到profile