-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
44 lines (40 loc) · 1.15 KB
/
handler.js
File metadata and controls
44 lines (40 loc) · 1.15 KB
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
var fs = require("fs");
var mditor = require("mditor");
var tp = require("tpjs");
var styleBuffer = fs.
readFileSync(require.resolve('mditor/dist/css/mditor.min.css'))
.toString();
var page = tp.compile(fs.readFileSync(__dirname + "/page.html").toString());
var ALLOWED_METHODS = ["GET"];
var Handler = module.exports = function(server) {
var self = this;
self.server = server;
self.configs = self.server.configs;
self.parser = new mditor.Parser();
};
//处理请求
Handler.prototype.handle = function(context) {
var self = this;
context.request.physicalPathExists(function(exists) {
if (exists) {
// markdown 只允许 get 请求
if (ALLOWED_METHODS.indexOf(context.request.method) < 0) {
context.notAllowed();
return;
}
fs.readFile(context.request.physicalPath, function(err, data) {
if (err) {
context.error(err);
} else {
var html = page({
style: styleBuffer,
body: self.parser.parse(data.toString())
});
context.send(html, self.server.mime('.html'));
}
});
} else {
self.next(context);
}
});
};