mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
allow marko/express to be used as middleware
This commit is contained in:
parent
29a4272a74
commit
64679b419a
21
express.js
21
express.js
@ -2,8 +2,24 @@ require('./runtime/env-init');
|
||||
|
||||
var assign = require('object-assign');
|
||||
var express = module.parent.require('express');
|
||||
var response = express.response;
|
||||
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(
|
||||
@ -30,5 +46,4 @@ response.marko = response.marko || function(template, data) {
|
||||
|
||||
template.render(data, res);
|
||||
};
|
||||
|
||||
delete require.cache[__filename];
|
||||
}
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
exports.createApp = function(express, markoExpressPath) {
|
||||
require(markoExpressPath);
|
||||
|
||||
var app = express();
|
||||
var markoExpress = require(markoExpressPath);
|
||||
|
||||
app.use(markoExpress());
|
||||
|
||||
return app;
|
||||
};
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user