Skip to content

Instantly share code, notes, and snippets.

@borestad
Created December 10, 2012 18:06
Show Gist options
  • Save borestad/4252208 to your computer and use it in GitHub Desktop.
Save borestad/4252208 to your computer and use it in GitHub Desktop.
========= server.coffee ===========
server.coffee
express = require 'express'
engines = require 'consolidate'
#sugar = require './assets/javascripts/vendor/sugar'
# Create ExpressServer
app = express()
exports.startServer = ((config) ->
# Main configuration
@configure ->
@set 'port', config.server.port
@set 'views', config.server.views.path
@engine config.server.views.extension, engines[config.server.views.compileWith]
@set 'view engine', config.server.views.extension
@use express.favicon()
@use express.bodyParser()
@use express.methodOverride()
@use express.compress()
@use config.server.base, @router
@use express.static(config.watch.compiledDir)
# Development configuration
@configure 'development', ->
@use express.errorHandler()
# Routes
require('./routes/index')(app, config)
# Setup server
server = @listen config.server.port, ->
console.log "Express server listening on port %d in %s mode", server.address().port, app.settings.env
server
).bind(app)
=============== routes/index.coffee ===============
##
# Routing root: '/'
#
module.exports = (app, config) -> (->
options =
reload: config.liveReload.enabled
optimize: config.isOptimize ? false
cachebust: if process.env.NODE_ENV isnt "production" then "?b=#{(new Date()).getTime()}" else ''
# In the event plain html pages are being used, need to
# switch to different page for optimized view
name = if config.isOptimize and config.server.views.html
"index-optimize"
else
"index"
# Routes
@get '/', (req, res) ->
res.render name, options
).bind(app)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment