[Express] req.next は場合により壊れている

Express で何か作る際に、探検好きな人はレスポンスオブジェクト res には リクエストオブジェクト req が入っていて、req には次に呼び出すべき関数に goto する関数 next が入っていることをご存知かと思います。

この req.next について、値はミドルウェア中では正しくてもリクエストルーター中ではちょっと間違っている (リクエストルーター自体を skip する next になっている) ように見えるので、保守的な方は常に引数として渡される方の next を使うようにしましょう。

var express = require('express'),
    app = express();

app.use(function (req, res, next) {
    console.log('middleware:', req.next === next, res.req.next === next);
    next();
});

app.get('/', function (req, res, next) {
    console.log('router:', req.next === next, res.req.next === next);
    // req.next(); doesn't work correctly.
    next();
}, function (req, res, next) {
    res.send('ok');
});

app.listen(3000);

/*
middleware: true true
router: false false
*/

コメント

このブログの人気の投稿

[linux] ping は通るのに No route to host と言われる

Chrome でダウンロードしたファイル名の一部がハイフンになる

ISUCON12 予選突破しました #isucon