-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (54 loc) · 2.01 KB
/
index.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
var md = require('node-markdown').Markdown,
static = require('node-static'),
routil = require('routil'),
fs = require('fs'),
http = require('http'),
path = require('path'),
url = require('url');
var gollum = function(wikiDirectory, port) {
// Checks if public directory exists
fs.exists(wikiDirectory, function (exists) {
if (!exists) {
console.error('Public directory does not exist.');
process.exit(1);
}
});
var file = new(static.Server)(wikiDirectory);
// Starts web server
http.createServer(function(req, res) {
var fileName = url.parse(req.url).pathname;
// If no URI specified, serve home.
if (fileName === '/') {
fileName = '/Home.md';
}
// If the request is for a markdown file
var filePath = path.join(wikiDirectory, fileName),
body = '';
// Checks if URL's md file exists
fs.exists(filePath, function (exists) {
if (exists) {
// Reading file
fs.readFile(filePath, function(error, data) {
if (error) {
var thatReq = req,
thatRes = res;
// try to find a static file to serve
file.serve(req, res, function (e, res) {
// If the file wasn't found, send 404
if (e && (e.status === 404)) {
routil.errorPage(thatReq, thatRes, 404);
}
});
} else {
// Converting file's markdown to HTML and serving it.
body = md(data.toString());
routil.sendHtml(res, body);
}
});
}
});
}).listen(port, function() {
console.log('node-gollum server started on port ' + port);
});
};
module.exports = gollum;