Created
October 21, 2011 23:14
-
-
Save ded/1305237 to your computer and use it in GitHub Desktop.
Rails-like routes for Express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
, app = express.createServer() | |
require('./config/routes').initialize(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
index: function (req, res, next) { | |
res.render('index', { | |
title: 'Express' | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
} |
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
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
Is require async now? I remember that a few versions back it wasn't.