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

ost*_*ard 110 node.js express

有没有之间的差异非常有用app.all('*', ... ),并app.use('/', ...)在Node.js的Express吗?

hun*_*tis 116

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

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

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

通常,如果您想对所有路由进行全局操作,app.use()是更好的选择.此外,它具有更少的未来错误机会,因为快递0.4可能会丢弃隐式路由器(意思是,中间件中路由器的位置将比现在更重要,因为您在技术上甚至不必使用它马上).

  • 这是否仍然适用于Express 4.x之后?app.router已删除. (14认同)
  • 你可以将 `next("route")` 与 `app.all` 一起使用,但不能与 `app.use` 一起使用。 (2认同)
  • 我进一步研究了@ musicin3d,发现此[GitHub问题](https://github.com/expressjs/express/issues/3133),证实了“ next()和next('route')与应用没有区别。使用”(引号)。他们应该更改文档。 (2认同)

Pal*_*ani 78

app.use只有一个回调函数,它适用于中间件.中间件通常不处理请求和响应(技术上他们可以),它们只处理输入数据,并将其移交给队列中的下一个处理程序.

app.use([path], function)
Run Code Online (Sandbox Code Playgroud)

app.all需要多次回调,并且用于路由.通过多个回调,您可以过滤请求并发送响应.它在express.js上的Filters中进行了解释

app.all(path, [callback...], callback)
Run Code Online (Sandbox Code Playgroud)

app.use只查看url是否以指定的路径开头

app.use( "/product" , mymiddleware);
// will match /product
// will match /product/cool
// will match /product/foo
Run Code Online (Sandbox Code Playgroud)

app.all将匹配完整路径

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
Run Code Online (Sandbox Code Playgroud)

  • 至少在[v4,app.use](http://expressjs.com/4x/api.html#app.use)中需要一个*或更多*中间件函数,而不是"只有一个". (15认同)
  • app.use只查看url是否以指定路径开头; app.all将匹配完整路径.这是主要的区别. (2认同)

dae*_*981 14

  • app.use:

    1. 将middlware注入前端控制器,例如:header,cookies,sessions等.
    2. 必须在app [http_method]之前写入,否则将无法执行.
    3. 几个调用按写入顺序处理
  • 惊恐:

    1. (如app [http_method])用于配置路由的控制器
    2. "all"表示它适用于所有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);
Run Code Online (Sandbox Code Playgroud)

这是访问路径'/ 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
Run Code Online (Sandbox Code Playgroud)

  • 在快递4.x上逐字运行此示例后,它实际上按顺序运行全部5.这可能是由于自写这篇文章以来差不多3年来快递的变化,但我只是想为了清晰起见而加上这一点. (5认同)

小智 11

使用时app.use(),"mount"路径被剥离,中间件功能不可见:

app.use('/static', express.static(__dirname + '/public'));
Run Code Online (Sandbox Code Playgroud)

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

app.all(),没有那种行为.


Gur*_*ngh 5

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

另一方面,app.use()用于您可能拥有的任何中间件,它安装到路径前缀上,并且每当请求该路由下的 URI 时都会调用它。

这是app.allapp.use的文档。