Tag: 中间件

除了特定的path,在Express中使用特定的中间件

我在node.js中使用了一些中间件function的Express框架: var app = express.createServer(options); app.use(User.checkUser); 我可以使用带有附加参数的.use函数来仅在特定path上使用此中间件: app.use('/userdata', User.checkUser); 是否有可能使用pathvariables,以便中间件用于除特定path之外的所有path,即根path? 我在想这样的事情: app.use('!/', User.checkUser); 所以User.checkUser总是被调用,除了根path。

在expressjs中使用next()将variables传递给下一个中间件

那么我的问题是我想通过一些variables从第一个中间件到另一个中间件,我尝试这样做,但有req.somevariable是一个给定的'未定义'。 //app.js .. app.get('/someurl/',middleware1,middleware2) … ////middleware1 … some conditions … res.somevariable = variable1; next(); … ////middleware2 … some conditions … variable = req.somevariable; … 谢谢回答。