1 | require("./");
|
2 |
|
3 | var express = module.parent
|
4 | ? module.parent.require("express")
|
5 | : require("express");
|
6 |
|
7 | patchResponse(express.response);
|
8 | delete require.cache[__filename];
|
9 |
|
10 | module.exports = function markoAppMiddleware() {
|
11 | var sacrificialApp = express();
|
12 |
|
13 | sacrificialApp.once("mount", function onmount(parent) {
|
14 |
|
15 | patchResponse(parent.response);
|
16 |
|
17 |
|
18 | if (parent._router) {
|
19 |
|
20 | parent._router.stack.pop();
|
21 | } else {
|
22 |
|
23 | parent.router.stack.pop();
|
24 | }
|
25 | });
|
26 |
|
27 | return sacrificialApp;
|
28 | };
|
29 |
|
30 | function patchResponse(response) {
|
31 | response.marko =
|
32 | response.marko ||
|
33 | function(template, data) {
|
34 | if (typeof template === "string") {
|
35 | throw new Error(
|
36 | "res.marko does not take a template name or path like res.render. " +
|
37 | "Instead you should use `require('./path/to/template.marko')` " +
|
38 | "and pass the loaded template to this function."
|
39 | );
|
40 | }
|
41 |
|
42 | var res = this;
|
43 | var req = res.req;
|
44 | var app = res.app;
|
45 | var $global = Object.assign({ app, req, res }, app.locals, res.locals);
|
46 |
|
47 | if (data) {
|
48 | data = Object.assign(data, {
|
49 | $global: Object.assign($global, data.$global)
|
50 | });
|
51 | } else {
|
52 | data = { $global };
|
53 | }
|
54 |
|
55 | res.set({ "content-type": "text/html; charset=utf-8" });
|
56 | return template.render(data, res).on("error", req.next);
|
57 | };
|
58 | }
|