1
app.get('/', (req,res,next) => {
req.foo = bar;
next();
}
しかし、どうすればライブラリや外部モジュールから実行できますか?
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
}
:次のモジュールを考えてみ
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())
// ...