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 中添加了一个调试行,以便我们知道它何时被调用。注意 /call/loginAnon 是在 bodyParser之前定义的。这是结果:

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"}

所以,我的回调最终被调用了两次——一次是在调用 bodyparser 之前(所以req.bodynull),一次是之后(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"}

还是叫了两次。但是,至少这次它设置了 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 回答