Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure app.configure in Express

I'm using Express 3.0 alpha to build an app but am a little confused with the app config structure

app.configure ->
  app.set "views", __dirname + "/views"
  app.set "view engine", "jade"
  app.use express.bodyParser()
  app.use express.methodOverride()

app.configure "development", ->
  app.use express.logger("dev")

app.configure "production", ->
  app.use express.logger()
  1. Is the first app.configure, -> required? I've been browsing other people's apps and it doesn't seem to matter if I use it.

  2. How does ordering work for app.configure, ->? It seems correct to put the specific environments (development, and production) after the first app.configure, -> as I've seen in other apps but it doesn't seem to work with my app (i.e. the logger doesn't print anything in my console at all).

Thanks in advance!

like image 262
Winter Avatar asked May 03 '12 00:05

Winter


People also ask

What is app use () in Express?

The app. use() method mounts or puts the specified middleware functions at the specified path. This middleware function will be executed only when the base of the requested path matches the defined path.


1 Answers

They're just executed in sequence. The first will be invoked for all environments, so it doesn't matter if it's wrapped with configure() at all; it just looks nicer. But if you look at the Express issue queue, they'll likely be disappearing in the future since they're effectively just glorified if statements.

like image 88
TJ Holowaychuk Avatar answered Sep 27 '22 20:09

TJ Holowaychuk