-
Notifications
You must be signed in to change notification settings - Fork 16
/
04-02.js
39 lines (35 loc) · 1.04 KB
/
04-02.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var connect = require("connect"),
fs = require("fs"),
mustache = require("mustache"),
requirejs = require("requirejs"),
parentTmpl;
requirejs.config({ nodeRequire: require });
connect(
connect.static(__dirname + "/public"),
connect.router(function(app) {
app.get("/show/:tmpl/:firstName/:lastName", function(req, res) {
var userName = {
firstName: req.params.firstName,
lastName: req.params.lastName
};
requirejs(["text!public/parent.html"], function(_parentTmpl) {
parentTmpl = _parentTmpl;
render(res, req.params.tmpl + ".html", userName);
});
});
})
).listen(8000);
function render(res, filename, data, style, script, callback) {
requirejs(["text!public/" + filename], function(tmpl) {
if (callback) {
callback(res, tmpl, data, style, script);
} else {
var html = mustache.to_html(
parentTmpl,
{content: data},
{content: tmpl, stylesheets: style || "", scripts: script || ""}
);
res.end(html);
}
});
}