Skip to content

Instantly share code, notes, and snippets.

@ded
Created October 21, 2011 23:14
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ded/1305237 to your computer and use it in GitHub Desktop.
Rails-like routes for Express
var express = require('express')
, app = express.createServer()
require('./config/routes').initialize(app)
module.exports = {
index: function (req, res, next) {
res.render('index', {
title: 'Express'
})
}
}
module.exports.initialize = function (app) {
function match(method, route, controller, action) {
app[method](route, function (req, res, next) {
require('../controllers/' + controller)[action || 'index'](req, res, next)
})
}
match('get', '/', 'application', 'index')
}
@xonecas
Copy link

xonecas commented Oct 22, 2011

Is require async now? I remember that a few versions back it wasn't.

@ded
Copy link
Author

ded commented Oct 22, 2011

they've always been sync as far as I know. With regards to this example, it definitely works, you should try it out on a default Express install

@skw
Copy link

skw commented Apr 11, 2013

express-coffee does something similar with express in CS, I'm kinda on the fence about using CS but it is a pleasure to work with...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment