1

予期していなかったことに気付きました。私はしばらくそれを「理解」しようとしてきましたが、うまく管理できませんでした。それは私を狂気に駆り立てています...

標準の Expressjs ファイルを使用します。私は何かを追加しました:

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , http = require('http')
  , path = require('path');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));

  app.post( '/call/loginAnon', function(req,res, next){
    console.log("IN THE CALL! %j %j %j", req.path, req.body, req.headers );
    next();
  });

  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});


app.get('/', routes.index);

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

また、bodyParser.js にデバッグ行を追加して、呼び出されたときにわかるようにしました。bodyParser の前に/call/loginAnon が定義されていることに注意してください。結果は次のとおりです。

IN THE CALL! "/call/loginAnon" undefined {"host":"localhost:3000","connection":"keep-alive","content-length":"30","origin":"http://localhost:3000","x-requested-with":"XMLHttpRequest","user-agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1","content-type":"application/json","accept":"application/javascript, application/json","referer":"http://localhost:3000/login","accept-encoding":"gzip,deflate,sdch","accept-language":"en-GB,en-US;q=0.8,en;q=0.6","accept-charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3","cookie":"sid=s%3ASI%2F9XFcpe2qFdQNrmsL8ihz8.MezqsOBJM%2FWX9BZOJ0za7F1d8UX6sNjmQZBhN3mqFVA"}

Headers IN BODYPARSER.js: /call/loginAnon {"host":"localhost:3000","connection":"keep-alive","content-length":"30","origin":"http://localhost:3000","x-requested-with":"XMLHttpRequest","user-agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1","content-type":"application/json","accept":"application/javascript, application/json","referer":"http://localhost:3000/login","accept-encoding":"gzip,deflate,sdch","accept-language":"en-GB,en-US;q=0.8,en;q=0.6","accept-charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3","cookie":"sid=s%3ASI%2F9XFcpe2qFdQNrmsL8ihz8.MezqsOBJM%2FWX9BZOJ0za7F1d8UX6sNjmQZBhN3mqFVA"}

IN THE CALL! "/call/loginAnon" {"login":"sd","password":"sd"} {"host":"localhost:3000","connection":"keep-alive","content-length":"30","origin":"http://localhost:3000","x-requested-with":"XMLHttpRequest","user-agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1","content-type":"application/json","accept":"application/javascript, application/json","referer":"http://localhost:3000/login","accept-encoding":"gzip,deflate,sdch","accept-language":"en-GB,en-US;q=0.8,en;q=0.6","accept-charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3","cookie":"sid=s%3ASI%2F9XFcpe2qFdQNrmsL8ihz8.MezqsOBJM%2FWX9BZOJ0za7F1d8UX6sNjmQZBhN3mqFVA"}

そのため、私のコールバックは2 回呼び出されました。1は bodyparser が呼び出される前 (そうreq.bodyですnull) と、その後 (req.body実際に設定されています)。

呼び出しを bodyParser の下に移動すると、多少改善されました。

Headers IN BODYPARSER.js: /call/loginAnon {"host":"localhost:3000","connection":"keep-alive","content-length":"30","origin":"http://localhost:3000","x-requested-with":"XMLHttpRequest","user-agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1","content-type":"application/json","accept":"application/javascript, application/json","referer":"http://localhost:3000/login","accept-encoding":"gzip,deflate,sdch","accept-language":"en-GB,en-US;q=0.8,en;q=0.6","accept-charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3","cookie":"sid=s%3ASI%2F9XFcpe2qFdQNrmsL8ihz8.MezqsOBJM%2FWX9BZOJ0za7F1d8UX6sNjmQZBhN3mqFVA"}

IN THE CALL! "/call/loginAnon" {"login":"sd","password":"sd"} {"host":"localhost:3000","connection":"keep-alive","content-length":"30","origin":"http://localhost:3000","x-requested-with":"XMLHttpRequest","user-agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1","content-type":"application/json","accept":"application/javascript, application/json","referer":"http://localhost:3000/login","accept-encoding":"gzip,deflate,sdch","accept-language":"en-GB,en-US;q=0.8,en;q=0.6","accept-charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3","cookie":"sid=s%3ASI%2F9XFcpe2qFdQNrmsL8ihz8.MezqsOBJM%2FWX9BZOJ0za7F1d8UX6sNjmQZBhN3mqFVA"}

IN THE CALL! "/call/loginAnon" {"login":"sd","password":"sd"} {"host":"localhost:3000","connection":"keep-alive","content-length":"30","origin":"http://localhost:3000","x-requested-with":"XMLHttpRequest","user-agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1","content-type":"application/json","accept":"application/javascript, application/json","referer":"http://localhost:3000/login","accept-encoding":"gzip,deflate,sdch","accept-language":"en-GB,en-US;q=0.8,en;q=0.6","accept-charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3","cookie":"sid=s%3ASI%2F9XFcpe2qFdQNrmsL8ihz8.MezqsOBJM%2FWX9BZOJ0za7F1d8UX6sNjmQZBhN3mqFVA"}

それでも2回呼ばれる。ただし、少なくとも今回は req.body が設定されていました。

それを app.configure の最後に移動すると、実際にはすべて修正されます。

Headers IN BODYPARSER.js: /call/loginAnon {"host":"localhost:3000","connection":"keep-alive","content-length":"30","origin":"http://localhost:3000","x-requested-with":"XMLHttpRequest","user-agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1","content-type":"application/json","accept":"application/javascript, application/json","referer":"http://localhost:3000/login","accept-encoding":"gzip,deflate,sdch","accept-language":"en-GB,en-US;q=0.8,en;q=0.6","accept-charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3","cookie":"sid=s%3ASI%2F9XFcpe2qFdQNrmsL8ihz8.MezqsOBJM%2FWX9BZOJ0za7F1d8UX6sNjmQZBhN3mqFVA"}

IN THE CALL! "/call/loginAnon" {"login":"sd","password":"sd"} {"host":"localhost:3000","connection":"keep-alive","content-length":"30","origin":"http://localhost:3000","x-requested-with":"XMLHttpRequest","user-agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1","content-type":"application/json","accept":"application/javascript, application/json","referer":"http://localhost:3000/login","accept-encoding":"gzip,deflate,sdch","accept-language":"en-GB,en-US;q=0.8,en;q=0.6","accept-charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3","cookie":"sid=s%3ASI%2F9XFcpe2qFdQNrmsL8ihz8.MezqsOBJM%2FWX9BZOJ0za7F1d8UX6sNjmQZBhN3mqFVA"}

これは、私が常に行ってきたことです (そして、ほとんどの人が常に行っていたことです)。構成する前にたまたまルートを定義し、奇妙なことに気付きました。

これは、ルートを定義するときにexpressJsがどのように機能するかを正しく理解していないためだと確信しています。しかし...誰かがこれにいくつかの光を当てることができますか?

ありがとう、

メルク。

4

1 に答える 1

5

パスが複数回アクセスされる理由は、app.routerミドルウェアの前にハンドラーを定義したためです。

その理由は、ミドルウェアが定義された順序で使用され、各レイヤーが要求に応答するか、次のレイヤーに送信するためです。だからあなたの例を説明する:

    // The handler is defined before the router so any request that passed the 
    // previous middleware will also be passed into this middleware
    // The method (GET or POST or..) or the path is irrelevant at this point
    app.post( '/call/loginAnon', function(req,res, next){
      // Will run every time
      console.log("IN THE CALL! %j %j %j", req.path, req.body, req.headers );
      next(); // It is sent to the next middleware layer
    });
    // Now the bodyParsel will parse the body and give you the req.body variable
    app.use(express.bodyParser()); 
    app.use(express.methodOverride());
    // This handler will now parse the path of the request and redirect it
    // to the defined handler which is why it is accessed the second time
    app.use(app.router);
于 2012-09-20T12:00:32.667 に答える