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

后端 未结 7 1904
谎友^
谎友^ 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:44

    app.use takes only one callback function and it's meant for Middleware. Middleware usually doesn't handle request and response, (technically they can) they just process input data, and hand over it to next handler in queue.

    app.use([path], function)
    

    app.all takes multiple callbacks, and meant for routing. with multiple callbacks you can filter requests and send responses. Its explained in Filters on express.js

    app.all(path, [callback...], callback)
    

    app.use only sees whether url starts with the specified path

    app.use( "/product" , mymiddleware);
    // will match /product
    // will match /product/cool
    // will match /product/foo
    

    app.all will match complete path

    app.all( "/product" , handler);
    // will match /product
    // won't match /product/cool   <-- important
    // won't match /product/foo    <-- important
    
    app.all( "/product/*" , handler);
    // won't match /product        <-- Important
    // will match /product/
    // will match /product/cool
    // will match /product/foo
    
    0 讨论(0)
提交回复
热议问题