简体   繁体   English

如何在Express中安装应用程序

[英]How does mounting apps in Express work

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. 所以我看过TJ的创建模块化Express-apps的指南 ,跟着它取得了很好的效果,但是想要了解它的工作原理的详细信息,但搜索没有给我任何答案。

In short I am wondering: When mounting apps in Express, what parts of the apps are shared and what parts are not? 总之,我想知道:在Express中安装应用程序时,应用程序的哪些部分是共享的,哪些部分不是?

Some examples to clarify my question: 一些例子来澄清我的问题:

app.js: app.js:

app.use(express.bodyParser()); 

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

app.listen(3000);

This example works. 这个例子有效。 But if I place the app.use(loginApi) before app.use(express.bodyParser()); 但是如果我在app.use(express.bodyParser());之前放置app.use(loginApi) app.use(express.bodyParser()); , the body parser will not be available in the loginApi subapp. ,身体解析器将无法在loginApi子应用程序中使用。 Why is that? 这是为什么?

Another example: 另一个例子:

submodule.js submodule.js

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

app.js app.js

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

Now in this example, If I understand it correctly, the /funtimes route will not be affected by the submodule middleware for all routes. 现在在这个例子中,如果我理解正确的话, /funtimes路由不会受到所有路由的子模块中间件的影响。 But what about the rest of the routes of app.js ? 但app.js的其他路线怎么样? Will they be affected? 他们会受到影响吗? And what if I add another module, will it be affected? 如果我添加另一个模块,它会受到影响吗?

Middleware runs in order (until one of the middlewares doesn't call next() ). 中间件按顺序运行(直到其中一个中间件不调用next() )。

If you use() your mounted app before use() ing bodyParser , the entire sub-app will run before bodyParser adds its properties. 如果您在use() bodyParser之前use()您安装的应用程序,整个子应用程序将在bodyParser添加其属性之前运行。

if I place the app.use(loginApi) before app.use(express.bodyParser()); 如果我在app.use(express.bodyParser())之前放置app.use(loginApi); , the body parser will not be available in the loginApi subapp. ,身体解析器将无法在loginApi子应用程序中使用。 Why is that? 这是为什么?

That's because of the way Express handles requests. 那是因为Express处理请求的方式。 Any incoming request starts at the top of the middleware stack, starting with app.use() stack. 任何传入的请求都从中间件堆栈的顶部开始,从app.use()堆栈开始。

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. 中间件只是具有函数签名function(req, res, next)函数,如果要将请求移交给后续函数,则调用next() ,或者自己发送响应。 You define a 'middleware chain' of a bunch of these functions (many are provided by express, like express.logger() and express.compress() .) 您定义了一堆这些函数的“中间件链”(许多是由express提供的,如express.logger()express.compress() 。)

So in the following scenario: 所以在以下场景中:

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

then an incoming request will first hit app.use(express.bodyParser()) , parsing req.body . 然后传入的请求将首先命中app.use(express.bodyParser()) ,解析req.body Then that function calls its internal next() , passing it to the next function in the middleware chain ( app.use(loginApi) ). 然后该函数调用其内部的next() ,将其传递给中间件链中的下一个函数( app.use(loginApi) )。 The loginApi app has its own middleware chain, but the requests already have req.body set from the outer app. loginApi应用程序有自己的中间件链,但请求已经从外部应用程序设置了req.body 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. 如果loginApi没有发送响应,请求将继续app.use(app.router) ,此时请求将转到您在外部应用程序中设置的路由功能。

So the answer is: A mounted app will have the middleware functions shared, which are placed before the line app.use(loginApi) 所以答案是:一个已安装的应用程序将共享中间件功能,这些功能放在行app.use(loginApi)

What you're asking about is middleware. 您要问的是中间件。 This confused me for a while. 这困惑了我一段时间。 Middleware are the functions that run in order to take a request in and serve back a response. 中间件是为了接收请求并回送响应而运行的功能。 app.use() takes a function as its only argument. app.use()将函数作为唯一参数。 That function manipulates the request in a consistent way. 该函数以一致的方式操作请求。

app.use is a lot like app.all("*"). app.use很像app.all(“*”)。

The order matters. 订单很重要。 For example, you might want to run a validator before serving the response. 例如,您可能希望在提供响应之前运行验证程序。

One thing I learned recently is that you can pass an array of middleware functions to a route. 我最近学到的一件事是你可以将一系列中间件函数传递给一个路径。 For example 例如

app.get("/whatever",[
    function(req,res,next}{
        ...validation...
        next();
    },
    function(req,res) {
        ...actions....
        res.send(200);
    }

]); ]);

The next callback tells express to run the next function in the middleware. 下一个回调告诉express在中间件中运行下一个函数。 Middleware can also modify the request object. 中间件还可以修改请求对象。 This is used a lot in authentication. 这在身份验证中经常使用。 For example, you'll see req.user getting defined from the database so in later middleware you'll be able to refer to properties of the user. 例如,您将看到从数据库中定义req.user,因此在以后的中间件中,您将能够引用用户的属性。 But, it can also be used for a ton of other stuff. 但是,它也可以用于其他很多东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM