File size: 1,057 Bytes
ac53235 | 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 | const http = require("http");
const fs = require("fs");
const path = require("path");
const root = __dirname;
const port = process.env.PORT || 4173;
const types = {
".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".svg": "image/svg+xml",
};
const server = http.createServer((req, res) => {
const urlPath = decodeURIComponent(req.url.split("?")[0]);
const filePath = path.join(root, urlPath === "/" ? "index.html" : urlPath);
if (!filePath.startsWith(root)) {
res.writeHead(403);
res.end("Forbidden");
return;
}
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end("Not found");
return;
}
res.writeHead(200, { "Content-Type": types[path.extname(filePath)] || "application/octet-stream" });
res.end(data);
});
});
server.listen(port, () => {
console.log(`StudyFlow mockup running at http://localhost:${port}`);
});
|