0

我将如何确保特定对象可用于我以 express 处理的每个请求?

var somethingImportant = "really important";
var app = express();

// This is just a hypothetical example of what I'm after...
app.mixThisInWithRequest({
    somethingImportant: somethingImportant
});

app.use(function (request, response, next) {
   console.log(request.somethingImportant);
});

鉴于上面的示例,是否有类似于该mixThisInWithRequest功能的设施?

4

1 回答 1

3

将其添加到request中间件中的对象中,在app.use您需要的时候尽早添加:

var somethingImportant = "really important";
var app = express();

app.use(function (request, response, next) {
    request.somethingImportant = somethingImportant;
    next();
});
app.use(function (request, response, next) {
   console.log(request.somethingImportant);
});
于 2013-01-12T14:58:51.550 回答