我正在使用 Express 3.x ,当我放
时express = require("express")
routes = require("./routes")
http = require("http")
path = require("path")
app = express()
app.configure ->
app.set "port", process.env.PORT or 3000
app.set "views", __dirname + "/views"
app.set "view engine", "jade"
app.use express.favicon()
app.use express.logger("dev")
app.use express.bodyParser()
app.use express.methodOverride()
app.use app.router
app.use express.static(path.join(__dirname, "public"))
app.use (req,res,next)->
res.locals.name = "Jesse"
next()
app.use "/", (req,res,next)->
res.locals.name = "Jesse"
app.configure "development", ->
app.use express.errorHandler()
app.get "/", routes.index
http.createServer(app).listen app.get("port"), ->
console.log "Express server listening on port " + app.get("port")
和我的查看是
extends layout
block content
h1= title
p Welcome to #{title}
p Hello Mr #{name}
我希望每次get请求进入时都会调用第一个中间件(app.use),但它不会发生。 express2.x有什么变化吗?
这是来自快递3.x目录,并且已注释
https://github.com/visionmedia/express/blob/master/examples/view-locals/index.js
由于
答案 0 :(得分:8)
以下是它的外观。您必须将中间件置于app.router
express = require("express")
routes = require("./routes")
http = require("http")
path = require("path")
app = express()
app.set "port", process.env.PORT or 3000
app.set "views", __dirname + "/views"
app.set "view engine", "jade"
app.use express.favicon()
app.use express.logger("dev")
app.use express.bodyParser()
app.use express.methodOverride()
app.use (req,res,next)->
res.locals.name = "Jesse"
next()
app.use app.router
app.use express.static(path.join(__dirname, "public"))
app.configure "development", ->
app.use express.errorHandler()
app.get "/", routes.index
http.createServer(app).listen app.get("port"), ->
console.log "Express server listening on port " + app.get("port")
答案 1 :(得分:3)
您可以尝试使用DEBUG标志运行脚本以查看正在进行的操作。 Express具有调试语句,用于显示已定义的路由以及如何处理请求。
$ DEBUG=* node app.js
答案 2 :(得分:1)
将app.use移动到顶部...它应该可以工作。 :)
答案 3 :(得分:0)
您必须将中间件置于 app.configure()部分之上。