Download server/static.ts from surekim/tars: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/spaces/surekim/tars/resolve/main/server/static.ts
- Command line
-
hf download hf://spaces/surekim/tars/server/static.ts
-
curl -L -o static.ts https://huggingface.co/spaces/surekim/tars/resolve/main/server/static.ts
1.42 kB
| import express, { type Express } from "express"; | |
| import fs from "fs"; | |
| import path, { dirname } from "path"; | |
| import { fileURLToPath } from "url"; | |
| // [์์ ] __dirname์ ES ๋ชจ๋ ๋ฐฉ์์ ๋ง๊ฒ ์์ ํ๊ฒ ์ ์ํฉ๋๋ค. | |
| const __filename = fileURLToPath(import.meta.url); | |
| const __dirname = dirname(__filename); | |
| export function serveStatic(app: Express) { | |
| // [์์ ] ํ๋ก์ ํธ ๊ตฌ์กฐ์ ๋ง์ถฐ ๋น๋๋ public ํด๋์ ๊ฒฝ๋ก๋ฅผ ์ ํํ ์ก์ต๋๋ค. | |
| // ๋ณดํต server ํด๋์ ํ ๋จ๊ณ ์(..)์ public์ด ์์นํฉ๋๋ค. | |
| const distPath = path.resolve(__dirname, "..", "public"); | |
| if (!fs.existsSync(distPath)) { | |
| console.error(`๋น๋ ํด๋๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค: ${distPath}`); | |
| // ์๋ฒ๊ฐ ์์ ์ฃฝ์ง ์๋๋ก ์๋ฌ๋ฅผ ๋์ง๋ ๋์ ๋ก๊ทธ๋ฅผ ๋จ๊น๋๋ค. | |
| return; | |
| } | |
| // 1. ์ ์ ํ์ผ(์ด๋ฏธ์ง, JS, CSS) ์๋น | |
| app.use(express.static(distPath)); | |
| // 2. [์์ ] ๋ชจ๋ ์์ฒญ์ index.html๋ก ๋ฆฌ๋ค์ด๋ ํธ (SPA ๋ฐฉ์) | |
| // ๋นํ์ค ๋ฌธ๋ฒ์ธ "/{*path}" ๋์ ํ์ค ๋ฌธ๋ฒ์ธ "*"์ ์ฌ์ฉํฉ๋๋ค. | |
| app.get("*", (req, res) => { | |
| // API ์์ฒญ์ด ์๋ ๊ฒฝ์ฐ์๋ง index.html์ ๋ณด๋ ๋๋ค. | |
| if (!req.path.startsWith("/api")) { | |
| res.sendFile(path.resolve(distPath, "index.html"), (err) => { | |
| if (err) { | |
| res.status(404).send("index.html ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค."); | |
| } | |
| }); | |
| } | |
| }); | |
| } |