const http = require("http");
|
const fs = require("fs");
|
const path = require("path");
|
|
const root = path.resolve(__dirname, "..", "outputs");
|
const port = 8088;
|
|
const types = {
|
".html": "text/html; charset=utf-8",
|
".css": "text/css; charset=utf-8",
|
".js": "application/javascript; charset=utf-8",
|
".json": "application/json; charset=utf-8",
|
};
|
|
const server = http.createServer((req, res) => {
|
const url = new URL(req.url, `http://${req.headers.host}`);
|
let pathname = decodeURIComponent(url.pathname);
|
if (pathname === "/") pathname = "/sales_crm_demo.html";
|
const file = path.resolve(root, `.${pathname}`);
|
|
if (!file.startsWith(root)) {
|
res.writeHead(403);
|
res.end("Forbidden");
|
return;
|
}
|
|
fs.readFile(file, (err, data) => {
|
if (err) {
|
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
|
res.end("Not found");
|
return;
|
}
|
res.writeHead(200, { "Content-Type": types[path.extname(file)] || "application/octet-stream" });
|
res.end(data);
|
});
|
});
|
|
server.listen(port, "0.0.0.0", () => {
|
console.log(`Sales CRM demo is running at http://0.0.0.0:${port}/`);
|
});
|