开发者

Defining multiple Express.js routes using path parameters

开发者 https://www.devze.com 2023-04-13 08:28 出处:网络
How can I make Express.js distinguish from the paths \"/1.1.1\"开发者_如何学JAVA and \"/login\" ?

How can I make Express.js distinguish from the paths "/1.1.1"开发者_如何学JAVA and "/login" ?

I am using the following code:

app.get('/:x?.:y?.:z?', function(req, res){

...

app.get('/login', function(req, res){


Routes are executed in the order they are added. So if you want your login route to take precedence, define it first.

Otherwise, in cases where you want to make decisions based on route, you can call the next() function from inside your handler like this:

app.get('/:x?.:y?.:z?', function(req, res, next){ // <== note the 'next' argument 
    if (!req.params.x && !req.params.y && !req.params.z) {
        next(); // pass control to the next route handler
    }
    ...
}

From the Express guide: "The same is true for several routes which have the same path defined, they will simply be executed in order until one does not call next() and decides to respond."

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号