简体   繁体   English

Node.js多个Express对象

[英]Node.js multiple express objects

I have been trying to understand how Expressjs framework for Node.js handles routes in multiple files with no luck. 我一直在尝试了解Expressjs的Node.js框架如何处理多个文件中的路由而没有运气。 My main index.js looks similar this: 我的主要index.js看起来类似于:

var express = require('express');
var app = express();
app.use('/route', require('./api/route'));
app.set('important_var', global_var);

In the last line I set one global variable for my application, which is needed by all other routes. 在最后一行中,我为我的应用程序设置了一个全局变量,所有其他路由都需要它。

In the second JS file './api/route.js', I do kinda the same thing: 在第二个JS文件“ ./api/route.js”中,我做了同样的事情:

var express = require('express');
var router = app.Router(); 
router.get('/path1', () => {...});

In the second file I need an important_var which was set on application level in index.js. 在第二个文件中,我需要在index.js中的应用程序级别上设置的Important_var。 How can I access it? 我该如何访问? Am I allowed to create new (if it is considered new) object with express()? 我可以使用express()创建新的(如果认为是新的)对象吗? Is variable app from index.js considered as singleton and won't be recreated? index.js中的可变应用是否被视为单例并且不会被重新创建? How should I handle these kind of situations? 我应该如何处理这种情况?

If I understand your question correctly then these are the requirements you are trying to meet: 如果我正确理解了您的问题,那么您正在尝试满足以下要求:

  • A main file that configures the server. 配置服务器的主文件。
  • The main file sets "global" values that can be accessed by various routes within the application. 主文件设置了“全局”值,可以通过应用程序内的各种路由进行访问。
  • A sub router file that can access the global values. 可以访问全局值的子路由器文件。

If those requirements are correct then the following is a way of meeting them the Express way. 如果这些要求是正确的,则以下是一种快速满足这些要求的方法。

Using app.set for storing app globals can lead to issues since it is for assigning an app setting value. 使用app.set来存储应用程序全局变量可能会导致问题,因为它是用于分配应用程序设置值的。 Instead, app globals can safely be stored to a app.locals which is specifically intended for this use case. 相反,可以将应用程序全局变量安全地存储到此使用案例专用的app.locals中。

So in your main file ( /index.js ): 因此,在您的主文件( /index.js )中:

const express = require('express');
const app = express();
const subRouter = require('./api/route');

app.locals.important_var = 'My App Global';

app.use('/route', subRouter);

Then to access the app global in your sub router file ( /api/route.js ) you would use the req.app.locals object which is a reference back to app.locals : 然后要访问子路由器文件( /api/route.js )中的全局应用程序,可以使用req.app.locals对象,该对象是对app.locals的引用:

const express = require('express');
const router = express.Router();

module.exports = router;

router.get('/path1', (req, res) => {
  console.log(req.app.locals.important_var);
  // ...
});

To access values set with app.set() , you need to be able to call app.get() and it needs to be the same app object that you used when you called app.set() . 要访问与设定值app.set()你需要能够调用app.get()它需要是相同app ,你使用时,你叫对象app.set() Each app is an Express object and it keeps its own state. 每个app都是一个Express对象,并保持自己的状态。

When create route handlers using a router, you can access the app object that a given route is associated with via req.app . 使用路由器创建路由处理程序时,您可以通过req.app访问与给定路由关联的app对象。 So, if you have code in a router you can access things on the app object like this: 因此,如果您在路由器中有代码,则可以像这样访问app对象上的内容:

router.get('/path1', () => {
    let data = req.app.get('important_var');
    // rest of route handler here
});

It may be more common to use middleware and set a property of your own on the req object that all route handlers could then access: 使用中间件并在所有路由处理程序随后可以访问的req对象上设置自己的属性可能更为常见:

const express = require('express');
const app = express();
// custom middleware
app.use(function(req, res, next) {
    req.myImportantVar = someCalculation(important_var);
    next();
});

app.use('/route', require('./api/route'));

Then, in a router, you could do this: 然后,在路由器中,您可以执行以下操作:

const router = require('express').Router();
router.get('/path1', () => {
     console.log(req.myImportantVar);
     // rest of route handler here
});

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

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