NodeJS/Express:什么是“app.use"? [英] NodeJS / Express: what is "app.use"?

查看:50
本文介绍了NodeJS/Express:什么是“app.use"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 NodeJS express 模块的 文档 中,示例代码有app.use(...).

In the docs for the NodeJS express module, the example code has app.use(...).

什么是use函数,它在哪里定义?

What is the use function and where is it defined?

推荐答案

app 对象在创建 Express 服务器时被实例化.它有一个中间件堆栈,可以在app.configure()(现在在 4.x 版中已弃用).

The app object is instantiated on creation of the Express server. It has a middleware stack that can be customized in app.configure()(this is now deprecated in version 4.x).

要设置中间件,您可以调用 app.use(<specific_middleware_layer_here>) 用于您要添加的每个中间件层(它可以对所有路径通用,或者仅在您的服务器处理的特定路径上触发),它会添加到您的Express 中间件堆栈.中间件层可以在use的多次调用中一层一层地添加,甚至可以一次调用串联起来.有关详细信息,请参阅use 文档.

To setup your middleware, you can invoke app.use(<specific_middleware_layer_here>) for every middleware layer that you want to add (it can be generic to all paths, or triggered only on specific path(s) your server handles), and it will add onto your Express middleware stack. Middleware layers can be added one by one in multiple invocations of use, or even all at once in series with one invocation. See use documentation for more details.

为了从概念上理解 Express Middleware,下面是我的应用程序中间件堆栈 (app.stack) 在将我的应用程序对象作为 JSON 记录到控制台时的样子:

To give an example for conceptual understanding of Express Middleware, here is what my app middleware stack (app.stack) looks like when logging my app object to the console as JSON:

stack: 
   [ { route: '', handle: [Function] },
     { route: '', handle: [Function: static] },
     { route: '', handle: [Function: bodyParser] },
     { route: '', handle: [Function: cookieParser] },
     { route: '', handle: [Function: session] },
     { route: '', handle: [Function: methodOverride] },
     { route: '', handle: [Function] },
     { route: '', handle: [Function] } ]

正如你可能推断出的那样,我调用了 app.use(express.bodyParser())app.use(express.cookieParser()) 等,它将这些快速中间件层"添加到中间件堆栈中.请注意,路由是空白的,这意味着当我添加这些中间件层时,我指定它们在任何路由上触发.如果我添加了一个仅在路径 /user/:id 上触发的自定义中间件层,它将在该中间件层对象的 route 字段中反映为字符串上面堆叠打印输出.

As you might be able to deduce, I called app.use(express.bodyParser()), app.use(express.cookieParser()), etc, which added these express middleware 'layers' to the middleware stack. Notice that the routes are blank, meaning that when I added those middleware layers I specified that they be triggered on any route. If I added a custom middleware layer that only triggered on the path /user/:id that would be reflected as a string in the route field of that middleware layer object in the stack printout above.

每一层本质上都是添加一个函数,专门处理通过中间件的流程.

例如通过添加bodyParser您可以确保您的服务器通过快速中间件处理传入的请求.所以,现在解析传入请求的正文是您的中间件在处理传入请求时所采取的程序的一部分——这一切都是因为您调用了 app.use(bodyParser).

E.g. by adding bodyParser, you're ensuring your server handles incoming requests through the express middleware. So, now parsing the body of incoming requests is part of the procedure that your middleware takes when handling incoming requests -- all because you called app.use(bodyParser).

这篇关于NodeJS/Express:什么是“app.use"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆