forked from JacksonTian/ping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.js
executable file
·76 lines (69 loc) · 2.46 KB
/
context.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var path = require("path");
var fs = require("fs");
var footprint = require("footprint");
var Context = function (request, response, session, framework) {
this.request = request;
this.response = response;
this.session = session;
this.framework = framework;
};
Context.prototype.none = function () {
this.response.writeHead(204);
this.response.end();
};
Context.prototype.renderJSON = function (jsonObj) {
this.response.setHeader("Content-Type", "application/json");
this.response.writeHead(200);
this.response.end(JSON.stringify(jsonObj));
};
Context.prototype.redirect = function (url) {
this.response.setHeader("Location", url);
this.response.writeHead(301);
this.response.end();
};
Context.prototype._renderView = function (viewEngine, template, data) {
var framework = this.framework,
request = this.request,
response = this.response;
try {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
response.write(viewEngine.template(template, data));
response.end();
} catch (ex) {
console.log(ex.message);
console.log(ex.stack);
framework.handler500(request, response, "Parse template error.");
}
};
Context.prototype.renderView = function (view, data) {
var context = this,
framework = context.framework,
request = context.request,
response = context.response;
// Get engine.
var viewEngine = footprint;
// Check cache.
viewEngine._cache = viewEngine._cache || {};
var template = viewEngine._cache[view];
if (template) {
context._renderView(viewEngine, template, data);
} else {
var filePath = path.join(__dirname, "views/", view);
path.exists(filePath, function (exists) {
if(!exists) {
framework.handler500(request, response, "This template file doesn't exist.");
} else {
fs.readFile(filePath, "utf8", function(err, file) {
if (err) {
framework.handler500(request, response, err);
} else {
viewEngine._cache[view] = file;
context._renderView(viewEngine, file, data);
}
});
}
});
}
};
Context.prototype.renderPartial = Context.prototype.renderView;
exports.Context = Context;