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

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

问题描述

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



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

解决方案

应用对象在创建Express服务器时被实例化。它具有可以在 中间件堆栈 -to-4.xrel =noreferrer> app.configure()(现在在版本4.x中已被弃用)



要设置中间件,可以调用 app.use(< specific_middleware_layer_here>) (它可以是所有路径的通用或触发仅在您的服务器处理的特定路径上),并且它将添加到您的 Express 中中间件堆栈。可以在多个调用使用中逐个添加中间件层,甚至可以一次性地一次调用一次。
请参阅 使用有关详细信息,请参阅文档



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

  stack:
[{route:'',handle:函数]},
{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())等,其中adde d这些将中间件层显示给中间件堆栈。请注意,路由为空,这意味着当我添加这些中间件层时,我指定它们在任何路由上被触发。如果我添加了一个只在路径 / user /:id 上触发的自定义中间件层,将在路由



例如通过添加 bodyParser ,您确保您的服务器通过快速中间件处理传入请求。因此,现在解析传入请求的正文是处理传入请求时中间件执行的过程的一部分 - 所有这些都是因为您调用 app.use(bodyParser)


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

What is the use function and where is it defined?

解决方案

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).

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.

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] } ]

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.

Each layer is essentially adding a function that specifically handles something to your flow through the middleware.

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天全站免登陆