Spaces:
Sleeping
Sleeping
Download artifacts/api-server/src/index.ts from Shaurya2020/unstable-nodev2: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/Shaurya2020/unstable-nodev2/resolve/main/artifacts/api-server/src/index.ts
- Command line
-
hf download hf://spaces/Shaurya2020/unstable-nodev2/artifacts/api-server/src/index.ts
-
curl -L -o index.ts https://huggingface.co/spaces/Shaurya2020/unstable-nodev2/resolve/main/artifacts/api-server/src/index.ts
1.04 kB
| import { createServer } from "http"; | |
| import app, { bares, wispHandler } from "./app"; | |
| import { logger } from "./lib/logger"; | |
| const rawPort = process.env["PORT"] ?? "3000"; | |
| const port = Number(rawPort); | |
| if (Number.isNaN(port) || port <= 0) throw new Error(`Invalid PORT: "${rawPort}"`); | |
| const server = createServer((req, res) => { | |
| for (const bare of bares) { | |
| if (bare.shouldRoute(req)) { bare.routeRequest(req, res); return; } | |
| } | |
| app(req, res); | |
| }); | |
| server.on("upgrade", (req, socket, head) => { | |
| const url = req.url ?? ""; | |
| // Wisp protocol (optional — needs wisp-server-node installed) | |
| if (url.startsWith("/api/wisp") && wispHandler) { | |
| wispHandler(req, socket, head); | |
| return; | |
| } | |
| // Bare servers | |
| for (const bare of bares) { | |
| if (bare.shouldRoute(req)) { bare.routeUpgrade(req, socket, head); return; } | |
| } | |
| socket.end(); | |
| }); | |
| server.listen(port, '0.0.0.0', (err?: Error) => { | |
| if (err) { logger.error({ err }, "Error listening on port"); process.exit(1); } | |
| logger.info({ port }, "Server listening"); | |
| }); | |