を
app.locals.use(myMiddleware(req,res,next){res.locals.uname = 'fresh'; next();})
を追加することを考えて、その後、任意の経路呼び出しにいた私が正しくあなたを理解していれば、あなたは、次の操作を行うことができます
app.configure(function(){
// default express config
app.use(function (req, res, next) {
req.custom = "some content";
next();
})
app.use(app.router);
});
app.get("/", function(req, res) {
res.send(req.custom)
});
すべてのルートでreq.custom変数を使用できるようになりました。あなたはルータの前にapp.use関数を置くことを確認してください!
編集:
OK次のあなたのミドルウェアを使用して、必要なルートでそれを指定するか:)試してみてください。
function myMiddleware(req, res, next) {
res.locals.uname = 'fresh';
next();
}
app.get("/", myMiddleware, function(req, res) {
res.send(req.custom)
});
またはあなたが "グローバル" に設定することができます
app.locals.uname = 'fresh';
// which is short for
app.use(function(req, res, next){
res.locals.uname = "fresh";
next();
});
を
いいえ私は別のルートを越えて関数を呼びたいと思いますし、app.localsが私を助けると思っています – coool
私の答えを編集 – zemirco
ありがとう。私が送信したgithubリンクをチェックした場合は、myMiddlewareが定義されていて、次にルート関数のapp.locals.use(myMiddleware)への呼び出しがmyMiddlewareを呼び出すことができます。 app.locals.useがエラーを投げるので、私はそれを行うことができません。 res.locals.use(私res.localsを理解する)とのコンセプトは何ですか..ありがとう再び – coool