olum-compiler 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/server.js +25 -14
- package/package.json +1 -1
package/bin/server.js
CHANGED
|
@@ -39,13 +39,17 @@ function serve(entry, port) {
|
|
|
39
39
|
|
|
40
40
|
const server = http.createServer(handler);
|
|
41
41
|
function handler(req, res) {
|
|
42
|
-
|
|
42
|
+
const urlPath = req.url.split("?")[0]; // strip query string before resolving
|
|
43
|
+
|
|
44
|
+
// serve the (ws-injected) index.html — used for "/" and as the history-mode SPA fallback
|
|
45
|
+
function serveIndex() {
|
|
43
46
|
let indexPath = entry + "/index.html";
|
|
44
47
|
if (!fs.existsSync(indexPath)) indexPath = entry + "/index.htm";
|
|
45
48
|
|
|
46
49
|
fs.readFile(indexPath, (err, content) => {
|
|
47
50
|
if (err) {
|
|
48
|
-
|
|
51
|
+
res.writeHead(500, { "content-type": "text/plain" });
|
|
52
|
+
res.end("index.html not found");
|
|
49
53
|
return reject(err);
|
|
50
54
|
}
|
|
51
55
|
const dom = new jsdom.JSDOM(content.toString()).window.document;
|
|
@@ -56,19 +60,26 @@ function serve(entry, port) {
|
|
|
56
60
|
res.end("<!DOCTYPE html>" + html.outerHTML);
|
|
57
61
|
});
|
|
58
62
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
63
|
+
|
|
64
|
+
if (urlPath === "/") return serveIndex();
|
|
65
|
+
|
|
66
|
+
// serve an existing static file with its mime type
|
|
67
|
+
const finalPath = getPath(entry, urlPath);
|
|
68
|
+
if (finalPath && fs.statSync(finalPath).isFile()) {
|
|
69
|
+
const ext = path.extname(finalPath).toLowerCase();
|
|
70
|
+
const obj = mimes.find(item => ext === item.ext.toLowerCase());
|
|
71
|
+
res.writeHead(200, { "Content-Type": obj ? obj.mime : "application/octet-stream" });
|
|
72
|
+
fs.createReadStream(finalPath).pipe(res);
|
|
73
|
+
return;
|
|
71
74
|
}
|
|
75
|
+
|
|
76
|
+
// history-mode SPA fallback: a request with no file extension is a client-side route
|
|
77
|
+
// (e.g. /card) — serve index.html and let the olum router handle it. Anything with an
|
|
78
|
+
// extension is a genuinely missing asset, so return a real 404 instead of hanging.
|
|
79
|
+
if (!path.extname(urlPath)) return serveIndex();
|
|
80
|
+
|
|
81
|
+
res.writeHead(404, { "content-type": "text/plain" });
|
|
82
|
+
res.end("404 Not Found: " + urlPath);
|
|
72
83
|
}
|
|
73
84
|
const log = colors("cyan", "Serving ") + colors("green", domain);
|
|
74
85
|
resolve({ server: server, wsPort: wsPort, PORT: PORT, log: log });
|