-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathstart.js
55 lines (47 loc) · 1.31 KB
/
start.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
"use strict";
const connect = require("connect");
const serveStatic = require("serve-static");
const Renderer = require("docsify-server-renderer");
const util = require("../util/index");
const chalk = require("chalk");
const LRU = require("lru-cache");
module.exports = function(path, configFile, port) {
const config = util.getConfig(configFile);
path = path || ".";
const renderer = new Renderer(config);
const server = connect();
const cached = new LRU(config.maxAge || 0);
server.use(function(req, res) {
serveStatic(path, { index: false })(req, res, function() {
if (
/\.(jpg|jpeg|gif|png|svg|ico|mp4|webm|ogg|ogv|js|css|md)(?:\?v=[0-9.]+)?$/.test(
req.url
)
) {
res.writeHead(404);
res.end();
}
Promise.resolve(cached.get(req.url) || renderer.renderToString(req.url))
.then(function(html) {
cached.set(req.url);
res.end(html);
})
.catch(function(err) {
console.error(err);
res.writeHead(404);
res.end();
});
});
});
server.listen(port || 4000);
const msg =
"\n" +
chalk.blue("[SSR]") +
" Serving " +
chalk.green(`${path}`) +
" now.\n" +
"Listening at " +
chalk.green(`http://localhost:${port}`) +
"\n";
console.log(msg);
};