当前位置 : 主页 > 网页制作 > Nodejs >

node.js – 使用express-namespace调用两次中间件的Express请求

来源:互联网 收集:自由互联 发布时间:2021-06-16
Node.js的(v0.10.15) 的package.json "dependencies": {"express": "~3.3.5","express-namespace": "~0.1.1",}, app.coffee ...app = module.exports = express()server = http.createServer(app)app.configure () - app.use express.compress() app.use ex
Node.js的(v0.10.15)

的package.json

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

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

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!

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

你见过那样的吗?

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