Difference between app.all('*') and app.use('/')

后端 未结 7 1906
谎友^
谎友^ 2020-11-29 15:51

Is there a useful difference between app.all(\'*\', ... ) and app.use(\'/\', ...) in Node.JS Express?

7条回答
  •  清酒与你
    2020-11-29 16:31

    There are two main differences:

    1. pattern matching (answer given by Palani)
    2. next(route) won't work inside the function body of middleware loaded using app.use . This is stated in the link from the docs:

    NOTE: next('route') will work only in middleware functions that were loaded by using the app.METHOD() or router.METHOD() functions.
    

    Link: http://expressjs.com/en/guide/using-middleware.html

    The working effect of next('route') can be seen from the following example:

    app.get('/',
    (req,res,next)=>{console.log("1");
    next(route); //The code here skips ALL the following middlewares
    }
    (req,res,next)=>{next();}, //skipped
    (req,res,next)=>{next();}  //skipped
    );
    
    //Not skipped
    app.get('/',function(req,res,next){console.log("2");next();});
    

提交回复
热议问题