-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.js
124 lines (95 loc) · 2.26 KB
/
serve.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const fs = require("fs");
module.exports = async (waw) => {
waw.host = (host, callback) => {
return (req, res, next) => {
if (
(!host || req.get('host').toLowerCase() === host.toLowerCase()) &&
typeof callback === 'function'
) {
callback(req, res, next);
} else {
next();
}
}
}
const dists = [];
waw.serve = (dir, opts = {}) => {
if (opts.host) {
opts.host = opts.host.toLowerCase();
}
dists.push({ dir, opts });
};
const content = {};
const files = {};
const get = (url, file) => {
waw.app.get(url, (req, res) => {
res.sendFile(file);
});
}
waw.url = (file, links, obj, host) => {
if (host) host = host.toLowerCase();
if (typeof links === "string") {
links = links.split(" ");
}
for (let i = links.length - 1; i >= 0; i--) {
if (links[i].indexOf('/:') > -1) {
get(links[i], file);
links.splice(i, 1);
}
}
let html = waw.wjst.renderFile(file, obj);
for (let i = 0; i < links.length; i++) {
links[i] = links[i].split("/").join("");
if (obj) {
content[(host || "") + links[i]] = html;
} else {
files[(host || "") + links[i]] = file;
}
}
};
const htmls = {};
waw.html = (url, code) => {
if (code) htmls[url] = code;
return htmls[url];
};
waw.render = function(file, obj, eject){
if (typeof eject === 'function') {
eject(obj);
}
return waw.wjst.renderFile(file, obj);
}
waw.use((req, res, next) => {
if (req.originalUrl.startsWith('/api/')) {
return next();
}
const host = (req.get("host") || "").toLowerCase();
if (req.url.indexOf(".") > -1) {
for (let i = 0; i < dists.length; i++) {
if (
dists[i].opts.host &&
dists[i].opts.host !== host
) {
continue;
}
let url = req.url;
if (dists[i].opts.prefix) {
if (req.url.indexOf(dists[i].opts.prefix) != 0) {
continue;
}
url = url.replace(dists[i].opts.prefix, "");
}
if (fs.existsSync(dists[i].dir + url)) {
return res.sendFile(dists[i].dir + url);
}
}
}
let url = req.url.split("/").join("");
if (content[host + url] || content[url]) {
return res.send(content[host + url] || content[url]);
}
if (files[host + url] || files[url]) {
return res.sendFile(files[host + url] || files[url]);
}
next();
});
};