allow marko/express to be used as middleware

This commit is contained in:
Michael Rawlings 2017-03-08 16:58:48 -08:00
parent 29a4272a74
commit 64679b419a
5 changed files with 53 additions and 37 deletions

View File

@ -2,33 +2,48 @@ require('./runtime/env-init');
var assign = require('object-assign');
var express = module.parent.require('express');
var response = express.response;
response.marko = response.marko || function(template, data) {
if(typeof template === 'string') {
throw new Error(
'res.marko does not take a template name or path like res.render. ' +
'Instead you should use `require(\'./path/to/template.marko\')` ' +
'and pass the loaded template to this function.'
);
}
var res = this;
var req = res.req;
var app = res.app;
var $global = assign({ app, req, res }, app.locals, res.locals);
if (data) {
data = assign(data, {
$global: assign($global, data.$global)
});
} else {
data = { $global };
}
res.set({ 'content-type': 'text/html; charset=utf-8' });
template.render(data, res);
};
patchResponse(express.response);
delete require.cache[__filename];
module.exports = function markoAppMiddleware() {
var sacrificialApp = express();
sacrificialApp.once('mount', function onmount(parent) {
// Patch the response
patchResponse(parent.response);
// Remove sacrificial express app
parent._router.stack.pop();
});
return sacrificialApp;
}
function patchResponse(response) {
response.marko = response.marko || function(template, data) {
if(typeof template === 'string') {
throw new Error(
'res.marko does not take a template name or path like res.render. ' +
'Instead you should use `require(\'./path/to/template.marko\')` ' +
'and pass the loaded template to this function.'
);
}
var res = this;
var req = res.req;
var app = res.app;
var $global = assign({ app, req, res }, app.locals, res.locals);
if (data) {
data = assign(data, {
$global: assign($global, data.$global)
});
} else {
data = { $global };
}
res.set({ 'content-type': 'text/html; charset=utf-8' });
template.render(data, res);
};
}

View File

@ -1,10 +1,10 @@
exports.createApp = function(express, markoExpressPath) {
var app = express();
require(markoExpressPath);
var markoExpress = require(markoExpressPath);
app.locals.foo = 'FOO';
app.use(markoExpress());
app.use(function(req, res, next) {
res.locals.bar = 'BAR';
next();

View File

@ -1,7 +1,8 @@
exports.createApp = function(express, markoExpressPath) {
require(markoExpressPath);
var app = express();
var markoExpress = require(markoExpressPath);
app.use(markoExpress());
return app;
};

View File

@ -1,10 +1,10 @@
exports.createApp = function(express, markoExpressPath) {
require(markoExpressPath);
var app = express();
var markoExpress = require(markoExpressPath);
app.locals.foo = 'FOO';
app.use(markoExpress());
app.use(function(req, res, next) {
res.locals.bar = 'BAR';
next();

View File

@ -1,12 +1,12 @@
exports.createApp = function(express, markoExpressPath) {
require(markoExpressPath);
var app = express();
var markoExpress = require(markoExpressPath);
app.locals.foo = 'APP';
app.locals.bar = 'APP';
app.locals.baz = 'APP';
app.use(markoExpress());
app.use(function(req, res, next) {
res.locals.foo = 'RES';
res.locals.bar = 'RES';