添加方法要求?
req.user
像req.assert
和req.user
一样将express-validation和passport方法添加到reqvariables中。 我知道我可以从一条路线里面做到这一点:
app.get('/', (req,res,next) => { req.foo = bar; next(); }
但是,我怎样才能从库或外部模块呢?
简而言之:你公开一个你的消费者需要app.use()
的处理函数。
这是一般的方法。 考虑以下模块:
module.exports = function myExtension() { return function myExtensionHandler(req, res, next) { // This is called for every request - you can extend req or res here req.myFun = myFun // Make sure to call next() in order to continue the // chain of handlers return next() } } function myFun() { // My magic function }
现在,从消费者的angular度来看:
const express = require('express') const myExtension = require('my-extension') const app = express() // Here you tell Express to use your extension for incoming requests app.use(myExtension()) // ...