I am using Express 3.x and when I put
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")
and my View is
extends layout
block content
h1= title
p Welcome to #{title}
p Hello Mr #{name}
I am expecting the first middleware(app.use) to get called every time the get request comes in, but it doesn't happen. Is there any change from express2.x?
This is from the express 3.x directory and it is commented
https://github.com/visionmedia/express/blob/master/examples/view-locals/index.js
Thanks
Here is how it should look. you have to place the middleware above 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")
You can try running your script using the DEBUG flag to see what is going on. Express has debug statements that show which routes have been defined and how requests are being handled.
$ DEBUG=* node app.js
You have to place your middleware above the app.configure() section.
Move the app.use to the top...and it should work. :)