3

node.js(v0.10.15)

包.json

"dependencies": {
"express": "~3.3.5",
"express-namespace": "~0.1.1",
},

应用程序.coffee

...
app = module.exports = express()
server = http.createServer(app)


app.configure () ->
  app.use express.compress()
  app.use express.bodyParser()
  app.use express.cookieParser()
  app.use express.favicon()
  app.use express.session({secret: '343453wEFsda'})
  app.use express.static( __dirname + '/public')
  app.use app.router
(require('./routes'))(app)
app.listen(3000);

路线.咖啡

test1 = (req, res,next) ->
  console.log("twice? what the...")
  next();

test = (req, res, next) ->
  console.log("once!");
  next();


routes = (app) ->
  app.namespace '/', test1,  ->
    app.get '/', test, (req, res) ->
      res.send('');

    app.post '/', (req, res) ->
      res.send('');

module.exports = routes

请求的服务器日志如下:

twice? what the...
twice? what the...
once!

它不知道为什么中间件在单个请求上调用两次?

你见过这样的吗?

4

1 回答 1

16

如果您使用浏览器对其进行测试,则您的浏览器可能会在查找 favicon.ico 时发出第二个请求。尝试使用 curl/wget。

于 2014-02-22T11:47:58.220 回答