Skip to content

Commit 1082f53

Browse files
update server.js
1 parent 3f02ee1 commit 1082f53

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

server.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ try { WebSocket = require('ws'); isWebSocketAvailable = true; console.log('WebSo
99
catch (e) { console.log('WebSocket support disabled'); }
1010

1111
const DIST_DIR = path.join(__dirname, 'dist');
12+
const STATIC_DIR = process.env.SERVE_DIR
13+
? path.join(__dirname, process.env.SERVE_DIR)
14+
: DIST_DIR;
1215
const isProduction = process.env.IS_PRODUCTION === 'true';
13-
if (isProduction && !fs.existsSync(DIST_DIR)) throw new Error('dist directory does not exist');
14-
const PORT = isProduction ? 3000 : (process.env.PORT || 3000);
16+
if (isProduction && !fs.existsSync(STATIC_DIR)) throw new Error(`Serve directory does not exist: ${STATIC_DIR}`);
17+
const PORT = process.env.PORT || 3000;
1518
const wsClients = new Set();
1619

1720
const mimeTypes = {
@@ -50,8 +53,8 @@ const server = http.createServer((req, res) => {
5053
let pathName = parsedUrl.pathname === '/' ? '/index.html' : parsedUrl.pathname;
5154
if (req.method === 'POST') { handlePostRequest(req, res, parsedUrl); return; }
5255
if (isProduction) {
53-
let filePath = path.join(DIST_DIR, pathName.replace(/^\/+/, ''));
54-
if (path.relative(path.resolve(DIST_DIR), path.resolve(filePath)).startsWith('..')) { res.writeHead(403); res.end('Forbidden'); return; }
56+
let filePath = path.join(STATIC_DIR, pathName.replace(/^\/+/, ''));
57+
if (path.relative(path.resolve(STATIC_DIR), path.resolve(filePath)).startsWith('..')) { res.writeHead(403); res.end('Forbidden'); return; }
5558
serveFile(filePath, res);
5659
} else { res.writeHead(404); res.end('Not found (dev mode)'); }
5760
});

0 commit comments

Comments
 (0)