I am trying to build an Express project in CoffeeScript. I am trying to make the app
variable global so that I can use to anywhere - read configuration settings from it.
So far, I have tried this -
In my app.coffee
file -
app = express()
app.configure ->
app.set 'host', 'localhost'
http.createServer(app).listen 8888, ->
console.log 'Server started'
exports.app = app
I want to access the host
variable set above in one of my routes file. So, I tried in my route handler,
exports.app.get('host') # I get this undefined
How to accomplish this? Do I have to require(app)
in my route file. app.coffee
is requiring the module in which the route is present, obviously for routing, that is,
app.get '/', 'route_handler'
Your route handler will receive a request and response objects. Both of them have a reference to the global app variable. From there you can pick up the value that you set via app.set 'host' through req.app.settings.host
app.get '/', (req, res) ->
console.log req.app.settings.host
# your code goes here