在Express中挂载应用程序如何工作 [英] How does mounting apps in Express work

查看:88
本文介绍了在Express中挂载应用程序如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我看过 TJ创建模块化Express-app的指南,但效果很好,但是想要进一步了解其工作方式的详细信息,但是搜索没有任何答案.

So I've seen TJ's guide to creating modular Express-apps, followed it to good effects, but want to know more about the details of how it works, however a search gives me no answers.

简而言之,我想知道:在Express中挂载应用程序时,哪些应用程序是共享的,哪些不是共享的?

In short I am wondering: When mounting apps in Express, what parts of the apps are shared and what parts are not?

一些例子可以澄清我的问题:

Some examples to clarify my question:

app.js:

app.use(express.bodyParser()); 

app.use(loginApi); //loginApi is an express app 

app.listen(3000);

此示例有效.但是,如果将app.use(loginApi)放在app.use(express.bodyParser());之前,主体解析器将在loginApi子应用程序中不可用.为什么会这样?

This example works. But if I place the app.use(loginApi) before app.use(express.bodyParser()); , the body parser will not be available in the loginApi subapp. Why is that?

另一个例子:

submodule.js

submodule.js

var app = module.exports = require('express')();
app.all('*', function(req, res, next){
    console.log('nifty middleware');
        next();
});

app.js

app.get('/funtimes', fn);
app.use(submodule); 
app.listen(3000);

现在在此示例中,如果我理解正确的话,/funtimes路由将不受所有路由的子模块中间件的影响.但是app.js的其余路由又如何呢?他们会受到影响吗?如果我添加另一个模块,它将受到影响吗?

Now in this example, If I understand it correctly, the /funtimes route will not be affected by the submodule middleware for all routes. But what about the rest of the routes of app.js ? Will they be affected? And what if I add another module, will it be affected?

推荐答案

如果我将app.use(loginApi)放在app.use(express.bodyParser())之前; ,正文解析器将在loginApi子应用程序中不可用.为什么会这样?

if I place the app.use(loginApi) before app.use(express.bodyParser()); , the body parser will not be available in the loginApi subapp. Why is that?

那是因为Express处理请求的方式.任何传入请求都从中间件堆栈的顶部开始,从app.use()堆栈开始.

That's because of the way Express handles requests. Any incoming request starts at the top of the middleware stack, starting with app.use() stack.

中间件是具有函数签名function(req, res, next)的简单函数,如果它们希望将请求传递给后续函数,或者调用自身发送响应,则调用next().您可以定义一系列功能的中间件链"(很多由Express提供,例如express.logger()express.compress().)

Middleware are simply functions that have the function signature function(req, res, next) which either call next() if they want to hand off the request to subsequent functions, or send a response themselves. You define a 'middleware chain' of a bunch of these functions (many are provided by express, like express.logger() and express.compress().)

因此,在以下情况下:

app.use(express.bodyParser());
var loginApi = require('./api/index.js')
app.use(loginApi);
app.use(app.router);

然后,传入的请求将首先命中app.use(express.bodyParser()),解析req.body.然后,该函数调用其内部next(),将其传递给中间件链中的下一个函数(app.use(loginApi)). loginApi应用程序具有其自己的中间件链,但是请求已从外部应用程序设置为req.body.如果loginApi没有发送响应,则请求将继续到app.use(app.router),此时请求将转到您在外部应用中设置的路由功能.

then an incoming request will first hit app.use(express.bodyParser()), parsing req.body. Then that function calls its internal next(), passing it to the next function in the middleware chain (app.use(loginApi)). The loginApi app has its own middleware chain, but the requests already have req.body set from the outer app. If the loginApi doesn't send a response, the request will continue to app.use(app.router) and at that point the requests will go to the routing functions you set up in your outer app.

所以答案是:一个已安装的应用程序将共享中间件功能,这些中间件功能位于app.use(loginApi)

So the answer is: A mounted app will have the middleware functions shared, which are placed before the line app.use(loginApi)

这篇关于在Express中挂载应用程序如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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