app.all('*')和app.use('/')之间的区别

在Node.JS Express中app.use('/', ...) app.all('*', ... )app.use('/', ...)有用?

在大多数情况下,他们将等同地工作。 最大的区别是中间件的应用顺序:

  • app.all()附加到应用程序的路由器,所以每当到达app.router中间件(处理所有的方法路由… GET,POST等)时都会使用它。

  • app.use()附加到应用程序的主要中间件堆栈,因此按照中间件指定的顺序使用它。 例如,如果你先把它放在首位,那将是第一件事。 如果你把它放在最后(在路由器之后),它通常不会被运行。

通常,如果你想对全部路由做全局的事情,app.use()是更好的select。 此外,它具有较less的未来错误的机会,因为快递0.4可能会降低隐式路由器(意味着,路由器在中间件中的位置将比现在更重要,因为你在技术上甚至不必使用它马上)。

app.use只有一个callback函数,它的意思是中间件。 中间件通常不处理请求和响应,(技术上他们可以)只处理input数据,并将其交给队列中的下一个处理程序。

 app.use([path], function) 

app.all采取多个callback,并意味着路由。 与多个callback你可以过滤请求和发送回应。 它在express.js上的Filters上进行了解释

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

app.use只能看到url是否以指定的path开始

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

app.all将匹配完整的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/cool // will match /product/foo 
  • app.use:

    1. 注入middlware到您的前端控制器configuration例如:标题,cookies,会话等
    2. 必须在应用程序[http_method]之前写入,否则将不会执行。
    3. 几个电话按照书面的顺序处理
  • 惊恐:

    1. (如app [http_method])用于configuration路由控制器
    2. “全部”意味着它适用于所有http方法。
    3. 几个电话按照书面的顺序处理

看看这个expressJs代码示例:

 var express = require('express'); var app = express(); app.use(function frontControllerMiddlewareExecuted(req, res, next){ console.log('(1) this frontControllerMiddlewareExecuted is executed'); next(); }); app.all('*', function(req, res, next){ console.log('(2) route middleware for all method and path pattern "*", executed first and can do stuff before going next'); next(); }); app.all('/hello', function(req, res, next){ console.log('(3) route middleware for all method and path pattern "/hello", executed second and can do stuff before going next'); next(); }); app.use(function frontControllerMiddlewareNotExecuted(req, res, next){ console.log('(4) this frontControllerMiddlewareNotExecuted is not executed'); next(); }); app.get('/hello', function(req, res){ console.log('(5) route middleware for method GET and path patter "/hello", executed last and I do my stuff sending response'); res.send('Hello World'); }); app.listen(80); 

以下是访问路由'/ hello'时的日志:

 (1) this frontControllerMiddlewareExecuted is executed (2) route middleware for all method and path pattern "*", executed first and can do stuff before going next (3) route middleware for all method and path pattern "/hello", executed second and can do stuff before going next (5) route middleware for method GET and path patter "/hello", executed last and I do my stuff sending response 

通过app.use() ,“mount”path被剥离,并且对于中间件function是不可见的:

 app.use('/static', express.static(__dirname + '/public')); 

除非req.url包含此前缀( /static ),否则不会调用挂载的中间件函数( express.static,此时将在调用该函数时将其剥离

app.all() ,没有这种行为。

是的,当使用任何types的请求方法(POST,GET,PUT或DELETE)请求特定的URI时, app.all()

另一方面, app.use()用于您可能拥有的任何中间件,并且它会挂载到path前缀上,并且只要请求该路由下的URI,就会被调用。

这里是app.all & app.use的文档。