I do not care to create an extra layer if that leave the code more beautiful. But I do not want to be doing estupides. I'm using Node.js + Express + CoffeeScript, and I would like to know if what I'm doing is good.
I'm breaking the start configuration in three parts:
- Apps (app.engine, app.use, app.set)
- Configuration (turn configuration global)
- Routes (create routes)
'use strict'
# Own modules
Apps = require './apps'
Configuration = require './configuration'
Database = require './database'
Routes = require './routes'
#
# Here i'm using a stair effect, where i break the configuration
# in several files and call each one passing the express instance.
Server =
constructor: (@app) ->
@initialize()
return
initialize: ->
Apps @app
Configuration @app
Routes @app
return
#
# Emulate class
module.exports = (app) -> Server.constructor app
In other file, I'm calling this file like this:
Server = require './server'
express = require 'express'
app = express()
Server app
I create the instance of Express and send to server. One observation is the last line of the server file:
module.exports = (app) -> Server.constructor app
I'm doing this because I want to emulate the object as a class. I don't want to use a new one because I'm doing this in several files, in which I have the constructor that assign properties to this.
In the server file, I'm calling Apps, Configuration and Routes passing the app, like this:
Apps @app
Configuration @app
Routes @app
return
I'm pretty much doing the same in this files.
Apps:
'use strict'
Engines = require './engines'
Setups = require './setups'
Uses = require './uses'
Apps =
constructor: (@app) ->
@initialize()
return
initialize: ->
Engines @app # Express engines
Setups @app # Express setups
Uses @app # Express uses
return
#
# Emulate class
module.exports = (app) -> Apps.constructor app
In the engines, setups and uses I'm configuring all express configurations.
Config file:
'use strict'
global.config = require('./' + (process.env.NODE_ENV or 'development'))
Configuration =
constructor: (@app) ->
@definyAsGlobal()
return
definyAsGlobal: ->
@app.locals = global.config
return
module.exports = (app) -> Configuration.constructor(app)
Is this bad? I like the idea, but maybe I'm doing it wrong.
I know that I can put all the app uses, engines and sets in just one file, but I like the modulate idea.