Skip to content

Commit ac0958a

Browse files
committed
fix: localhost:8000/src should work without trailing /
1 parent d056660 commit ac0958a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

serve-proxy.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
/* eslint-env node */
23

34
const http = require('http');
45
const https = require('https');
@@ -68,9 +69,8 @@ proxy.on('error', (err, req, res) => {
6869
});
6970

7071
// Modify proxy request headers
71-
proxy.on('proxyReq', (proxyReq, req, res) => {
72+
proxy.on('proxyReq', (proxyReq, req) => {
7273
// Transform localhost:8000 to appear as phcode.dev domain
73-
const originalHost = req.headers.host;
7474
const originalReferer = req.headers.referer;
7575
const originalOrigin = req.headers.origin;
7676

@@ -293,7 +293,18 @@ const server = http.createServer((req, res) => {
293293
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}${config.logIp ? ` (${clientIp})` : ''}`);
294294
}
295295

296-
serveStaticFile(req, res, filePath);
296+
// Handle directory requests without trailing slash
297+
fs.stat(filePath, (err, stats) => {
298+
if (err) {
299+
serveStaticFile(req, res, filePath);
300+
} else if (stats.isDirectory() && !parsedUrl.pathname.endsWith('/')) {
301+
// Redirect to URL with trailing slash for directories
302+
res.writeHead(301, { 'Location': req.url + '/' });
303+
res.end();
304+
} else {
305+
serveStaticFile(req, res, filePath);
306+
}
307+
});
297308
});
298309

299310
// Parse arguments and start server

0 commit comments

Comments
 (0)