Created
September 3, 2014 01:34
-
-
Save sethkinast/d7c7938cc9dd529099c9 to your computer and use it in GitHub Desktop.
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
// Option 1: require inline | |
var app = express(); | |
app.get('/', require('routes/index')); | |
app.get('/login', require('routes/login').index); | |
app.post('/login', require('routes/login').do_login); | |
// Easy to split up routes | |
// Centralized route paths | |
// However, potentially lots of duplication | |
// Option 2: pass an app around | |
var app = express(); | |
require('routes/index')(app); | |
require('routes/login')(app); | |
//== routes/index | |
module.exports = function(app) { | |
app.get('/', index) | |
} | |
function index(req, res) { | |
// ... | |
} | |
//== routes/login | |
module.exports = function(app) { | |
app.get('/login', index); | |
app.post('/login', do_login); | |
} | |
function index(req, res) { } | |
function do_login(req, res) { } | |
// Nicely encapsulates different sections of the site | |
// Harder to find exactly which route file handles which path | |
// Too bad /login gets repeated once for every route... | |
// Express 4: Routers! | |
var app = express(); | |
var index = require('routes/index'), | |
login = require('routes/login'); | |
app.use('/', index); | |
app.use('/login', login); | |
//== routes/index | |
var router = module.exports = express.Router(); | |
router.get('/', index); | |
function index() { } | |
//== routes/login | |
var router = module.exports = express.Router(); | |
// Relative to this router. We can mount it to some prefix that could change easily! | |
router.get('/', index); | |
router.post('/', do_login); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment