| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- const fs = require("node:fs");
- const http = require("node:http");
- const path = require("node:path");
- const root = process.cwd();
- const contentTypes = {
- ".css": "text/css; charset=utf-8",
- ".html": "text/html; charset=utf-8",
- ".js": "text/javascript; charset=utf-8",
- ".json": "application/json; charset=utf-8",
- ".svg": "image/svg+xml",
- ".woff2": "font/woff2"
- };
- http
- .createServer((request, response) => {
- response.setHeader("Access-Control-Allow-Origin", "*");
- if (request.method === "OPTIONS") {
- response.writeHead(204);
- response.end();
- return;
- }
- let pathname;
- try {
- const url = new URL(request.url, "http://127.0.0.1");
- pathname = url.pathname === "/" ? "/index.html" : url.pathname;
- pathname = decodeURIComponent(pathname);
- } catch {
- response.writeHead(400);
- response.end();
- return;
- }
- const file = path.resolve(root, `.${pathname}`);
- if (!file.startsWith(`${root}${path.sep}`)) {
- response.writeHead(403);
- response.end();
- return;
- }
- fs.readFile(file, (error, content) => {
- if (error) {
- response.writeHead(error.code === "ENOENT" ? 404 : 500);
- response.end();
- return;
- }
- response.writeHead(200, {
- "Content-Type": contentTypes[path.extname(file)] || "application/octet-stream"
- });
- response.end(content);
- });
- })
- .listen(18184, "127.0.0.1");
|