huangning
2026-07-18 c8c5a62d62b9ad05cfa0b5e026496b56a735a18f
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
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}/`);
});