|
| 1 | +const http = require('http'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const PORT = process.env.PORT || 3000; |
| 6 | +const API_BASE_URL = (process.env.API_BASE_URL || '').replace(/\/$/, ''); |
| 7 | +const PUBLIC_DIR = __dirname; |
| 8 | + |
| 9 | +const contentTypes = { |
| 10 | + '.html': 'text/html; charset=utf-8', |
| 11 | + '.css': 'text/css; charset=utf-8', |
| 12 | + '.js': 'application/javascript; charset=utf-8', |
| 13 | + '.json': 'application/json; charset=utf-8', |
| 14 | + '.svg': 'image/svg+xml', |
| 15 | + '.png': 'image/png', |
| 16 | + '.jpg': 'image/jpeg', |
| 17 | + '.jpeg': 'image/jpeg', |
| 18 | + '.ico': 'image/x-icon', |
| 19 | +}; |
| 20 | + |
| 21 | +function send(res, statusCode, body, contentType = 'text/plain; charset=utf-8') { |
| 22 | + res.writeHead(statusCode, { 'Content-Type': contentType }); |
| 23 | + res.end(body); |
| 24 | +} |
| 25 | + |
| 26 | +function readFile(filePath) { |
| 27 | + return fs.readFileSync(filePath); |
| 28 | +} |
| 29 | + |
| 30 | +function serveFile(res, filePath) { |
| 31 | + try { |
| 32 | + const ext = path.extname(filePath).toLowerCase(); |
| 33 | + const contentType = contentTypes[ext] || 'application/octet-stream'; |
| 34 | + const body = readFile(filePath); |
| 35 | + res.writeHead(200, { 'Content-Type': contentType }); |
| 36 | + res.end(body); |
| 37 | + } catch { |
| 38 | + send(res, 404, 'Not found'); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +const server = http.createServer((req, res) => { |
| 43 | + const urlPath = new URL(req.url, `http://${req.headers.host}`).pathname; |
| 44 | + |
| 45 | + if (urlPath === '/config.js') { |
| 46 | + const config = `window.API_BASE_URL = ${JSON.stringify(API_BASE_URL)};\n`; |
| 47 | + return send(res, 200, config, 'application/javascript; charset=utf-8'); |
| 48 | + } |
| 49 | + |
| 50 | + const safePath = urlPath === '/' ? '/index.html' : urlPath; |
| 51 | + const filePath = path.join(PUBLIC_DIR, safePath); |
| 52 | + |
| 53 | + if (!filePath.startsWith(PUBLIC_DIR)) { |
| 54 | + return send(res, 403, 'Forbidden'); |
| 55 | + } |
| 56 | + |
| 57 | + if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) { |
| 58 | + return serveFile(res, filePath); |
| 59 | + } |
| 60 | + |
| 61 | + if (urlPath === '/' || urlPath === '/index.html') { |
| 62 | + return serveFile(res, path.join(PUBLIC_DIR, 'index.html')); |
| 63 | + } |
| 64 | + |
| 65 | + send(res, 404, 'Not found'); |
| 66 | +}); |
| 67 | + |
| 68 | +server.listen(PORT, () => { |
| 69 | + console.log(`Frontend running at http://localhost:${PORT}`); |
| 70 | +}); |
0 commit comments