Spaces:
Sleeping
Sleeping
Download server.js from polats/test-space: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/polats/test-space/resolve/main/server.js
- Command line
-
hf download hf://spaces/polats/test-space/server.js
-
curl -L -o server.js https://huggingface.co/spaces/polats/test-space/resolve/main/server.js
1.03 kB
| import http from "node:http"; | |
| import fs from "node:fs"; | |
| import path from "node:path"; | |
| import { fileURLToPath } from "node:url"; | |
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | |
| const PUBLIC = path.join(__dirname, "public"); | |
| const PORT = Number(process.env.PORT) || 7860; | |
| const MIME = { | |
| ".html": "text/html", | |
| ".js": "text/javascript", | |
| ".css": "text/css", | |
| }; | |
| const server = http.createServer((req, res) => { | |
| const urlPath = decodeURIComponent(new URL(req.url, "http://localhost").pathname); | |
| let filePath = path.join(PUBLIC, urlPath === "/" ? "index.html" : urlPath); | |
| if (!filePath.startsWith(PUBLIC)) { | |
| res.writeHead(403).end("Forbidden"); | |
| return; | |
| } | |
| fs.readFile(filePath, (err, data) => { | |
| if (err) { | |
| res.writeHead(404).end("Not found"); | |
| return; | |
| } | |
| res.writeHead(200, { "Content-Type": MIME[path.extname(filePath)] || "application/octet-stream" }); | |
| res.end(data); | |
| }); | |
| }); | |
| server.listen(PORT, "0.0.0.0", () => { | |
| console.log(`Server listening on port ${PORT}`); | |
| }); |