Skip to content

Instantly share code, notes, and snippets.

@emerleite
Forked from laurie71/A~debug.js
Created March 9, 2011 19:22
Show Gist options
  • Save emerleite/862782 to your computer and use it in GitHub Desktop.
Save emerleite/862782 to your computer and use it in GitHub Desktop.
// path/to/module/debug.js
var express = require('express');
exports.diagnostics = function() {
var app1 = express.createServer();
app1.configure(function() {
this.set('view engine', 'ejs');
this.set('views', __dirname + '/views/app1');
this.set('view options', { layout: false, open: '{%', close: '%}' });
});
app1.mounted(function(host) {
host.dynamicHelpers({
app1inc: function(req, res) {
return function(ctx) {
// XXX res.app is app1, so this fails...
// the intention is that the app we're mounted in
// should get the content from our bundled include
return res.partial('_include.ejs', { object: ctx, as: 'foo' });
}
}
});
});
app1.get('/', function(req, res) { res.render('index.ejs'); });
return app1;
};
<!-- path/to/module/views/index.ejs -->
<!-- served at: http://localhost:3000/debug/index.ejs -->
<div>packaged app index</div>
<!-- this partial works fine: -->
<%- partial('include.ejs', { object: { test: 'hosted' }, as: 'foo' }) %>
<!-- path/to/module/views/partials/include.ejs -->
<div>packed app include: foo.test is {%= foo.test %}</div>
// ./apps.js: main app
var express = require('express');
var debug = require('debug');
var app2 = express.createServer();
app2.configure(function() {
this.set('view engine', 'ejs');
this.set('views', __dirname + '/views/app2');
this.set('view options', { layout: false });
});
// uses packaged 'debug' app
app2.use('/debug', debug.diagnostics());
app2.get('/', function(req, res) { res.render('index.ejs'); });
if (! module.parent) {
app2.listen(3000);
console.log("Express server listening on port %d", app2.address().port);
}
<!-- ./views/index.ejs -->
<h1>App 2</h1>
<p><%- app1inc({ test: 'app2' }) %></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment