Last Updated: February 25, 2016
· dylanlukes

Hot CoffeeScript Express Boilerplate

Tired of extra syntax in your Express app.js, or want to switch to CoffeeScript for your server-side code? Try this pseudo-DSL on for size:


coffee = require('coffee-script');
http   = require('http');
app    = require('./app');

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));

express = require 'express'
_       = require 'underscore'
path    = require 'path'

routes = require './routes'
user   = require './routes/user'

module.exports = app = express()

do _.bind ->
    @configure ->
      @set('port', process.env.PORT || 3000)
      @set('views', __dirname + '/views')
      @set('view engine', 'jade')
      @use(express.static(path.join(__dirname, 'public')))

    @configure 'development', ->

    @get('/', routes.index)
    @get('/users', user.list)
  , app

Watch out for the indentation. The second argument to bindexpress() – is indented once, but the contents of the function are indented twice.

1 Response
Add your response

In coffeescript you can use do => (fat arrow to automatically perform the binding.

over 1 year ago ·