Skip to content
Myr

Added `route-loading` example for another route loading technique

Before adoption: this commit came in with the history the project adopted, so no landing records it.

using the vm module you can expose the route files at the root level which IMO is nicer than module.exports = function(app){ etc

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 388b84e · Browse files · 9 files+102 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+102 added−0 removed

  • other:+102 added−0 removed

9 files+102 added−0 removed

added:examples/route-loading/routes/main.js+4 added
added:examples/route-loading/routes/user.js+20 added
added:examples/route-loading/views/user/add.jade+5 added
1h2 Add a user2form(action='/user', method='post')3 p: input(type='text', name='user[name]', placeholder='Username')4 p: input(type='text', name='user[email]', placeholder='Email')5 p: input(type='submit', value='Add')
added:examples/route-loading/views/user/index.jade+6 added
1h1= user.name2table3 tbody4 tr5 td Email6 td= user.email
added:examples/route-loading/views/user/list.jade+5 added
1h1 Users2- if (users.length)3 != partial('user', users)4- else5 p No users, head over to <a href='/user/add'>/user/add</a> to create one.
added:examples/route-loading/views/index.jade+4 added
1h2 Route sharing example2ul3 li: a(href='/user/add') Add user4 li: a(href='/users') User list
added:examples/route-loading/views/layout.jade+11 added
1doctype html2html3 head4 title Route loading example5 style6 body {7 padding: 50px;8 font: 14px/1.5 solid helvetica, arial, sans-serif;9 }10 body11 #content!= body
added:examples/route-loading/app.js+21 added
added:examples/route-loading/boot.js+26 added
12/**3 * Module dependencies.4 */56var vm = require('vm')7 , fs = require('fs');89module.exports = function(app, db){10 var dir = __dirname + '/routes';11 // grab a list of our route files12 fs.readdirSync(dir).forEach(function(file){13 var str = fs.readFileSync(dir + '/' + file, 'utf8');14 // inject some pseudo globals by evaluating15 // the file with vm.runInNewContext()16 // instead of loading it with require(). require's17 // internals use similar, so dont be afraid of "boot time".18 var context = { app: app, db: db };19 // we have to merge the globals for console, process etc20 for (var key in global) context[key] = global[key];21 // note that this is essentially no different than ... just using22 // global variables, though it's only YOUR code that could influence23 // them, which is a bonus.24 vm.runInNewContext(str, context, file);25 });26};