41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
const http = require("http");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const root = __dirname;
|
|
const port = Number(process.env.PORT || 4173);
|
|
const types = {
|
|
".html": "text/html;charset=utf-8",
|
|
".css": "text/css;charset=utf-8",
|
|
".js": "text/javascript;charset=utf-8",
|
|
};
|
|
|
|
const server = http.createServer((request, response) => {
|
|
const url = new URL(request.url, `http://localhost:${port}`);
|
|
const requestedPath = url.pathname === "/" ? "index.html" : url.pathname.slice(1);
|
|
const filePath = path.resolve(root, requestedPath);
|
|
|
|
if (!filePath.startsWith(root)) {
|
|
response.writeHead(403);
|
|
response.end("Forbidden");
|
|
return;
|
|
}
|
|
|
|
fs.readFile(filePath, (error, buffer) => {
|
|
if (error) {
|
|
response.writeHead(404);
|
|
response.end("Not found");
|
|
return;
|
|
}
|
|
|
|
response.writeHead(200, {
|
|
"Content-Type": types[path.extname(filePath)] || "application/octet-stream",
|
|
});
|
|
response.end(buffer);
|
|
});
|
|
});
|
|
|
|
server.listen(port, "127.0.0.1", () => {
|
|
console.log(`Dental 3D demo: http://localhost:${port}`);
|
|
});
|