-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrakdown_server.js
65 lines (59 loc) · 2.21 KB
/
mrakdown_server.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
//调用了哪些模块和文件
var express = require('express')
var app = express();
var path = require('path');
var c = require('./config/config');
var log = eval("c.config." + c.config.config + ".log");
var box = require('./controler/box');
var markdown = require("markdown").markdown;
var rf = require("fs");
//初始化定义
app.configure(function () {
app.set('views', __dirname + '/ejs_public');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('zuaa'));
app.use(express.session());
app.use(app.router);
app.use(express.errorHandler());
});
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser('box'));
app.use(express.session({
cookie: {
maxAge: 2 * 60 * 1000
},
secret: "zuaasecretkey"
}));
app.get('/:p1', function (request, response) {
m(request.params.p1, request.params.p1, response)
});
app.get('/:p2/:p1', function (request, response) {
m(request.params.p2 + "/" + request.params.p1, request.params.p1, response)
});
app.get('/:p3/:p2/:p1', function (request, response) {
m(request.params.p3 + "/" + request.params.p2 + "/" + request.params.p1, request.params.p1, response)
});
app.get('/:p4/:p3/:p2/:p1', function (request, response) {
m(request.params.p4 + "/" + request.params.p3 + "/" + request.params.p2 + "/" + request.params.p1, request.params.p1, response)
});
app.get('/:p5/:p4/:p3/:p2/:p1', function (request, response) {
m(request.params.p5 + "/" + request.params.p4 + "/" + request.params.p3 + "/" + request.params.p2 + "/" + request.params.p1, request.params.p1, response)
});
function m(path, title, response) {
log.debug('hi markdown, i am here %s.', __dirname);
var data = rf.readFileSync(__dirname + "/mrakdown/" + path + ".md", "utf-8");
console.log(data);
var t = getHtmlTemplate().toString();
console.log(t)
data = markdown.toHTML(data)
t = t.toString();
t = t.replace('mkTitle', title)
t = t.replace('mkContent', data)
response.send(t);
}
function getHtmlTemplate() {
return rf.readFileSync(__dirname + "/mrakdown/template.html", "utf-8");
}
app.listen(process.env.port || 18080);
console.log(process.env.port || 18080);