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:
run.js
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'));
});
app.coffee
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.favicon())
@use(express.logger('dev'))
@use(express.bodyParser())
@use(express.methodOverride())
@use(@router)
@use(express.static(path.join(__dirname, 'public')))
@configure 'development', ->
@use(express.errorHandler())
@get('/', routes.index)
@get('/users', user.list)
, app
Watch out for the indentation. The second argument to bind – express() – is indented once, but the contents of the function are indented twice.
Written by Dylan Lukes
Related protips
1 Response
In coffeescript you can use do => (fat arrow http://coffeescript.org/#fat-arrow) to automatically perform the binding.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Coffeescript
Authors
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#