用express-namespace调用两次调用中间件的Express请求 [英] Express request with middleware called twice with express-namespace

查看:48
本文介绍了用express-namespace调用两次调用中间件的Express请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

node.js(v0.10.15)

node.js(v0.10.15)

package.json

package.json

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

app.coffee

app.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);

routes.coffee

routes.coffee

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

请求的服务器日志如下:

server logs of request like below:

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

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

It doesn't know why middleware called twice on single request?

你看到那样吗?

推荐答案

如果使用浏览器进行测试,则浏览器在查找favicon.ico时可能会再次发出请求.尝试使用curl/wget.

If you are using a browser to test it, it is possible that your browser makes a second request when it looks for favicon.ico. Try using curl/wget.

这篇关于用express-namespace调用两次调用中间件的Express请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆