mirror of
https://github.com/oven-sh/bun
synced 2026-02-14 04:49:06 +00:00
Fix DirectoryRoute path matching with wildcard pattern
- Register DirectoryRoute with /* pattern to match all sub-paths - This should fix 404 errors when accessing files in directory routes - For root path /, register as /* to catch all paths - For other paths, register as path/* to match sub-paths 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -2646,7 +2646,13 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d
|
||||
ServerConfig.applyStaticRoute(any_server, ssl_enabled, app, *FileRoute, file_route, entry.path, entry.method);
|
||||
},
|
||||
.directory => |directory_route| {
|
||||
ServerConfig.applyStaticRoute(any_server, ssl_enabled, app, *DirectoryRoute, directory_route, entry.path, entry.method);
|
||||
// For directory routes, we need to handle all sub-paths, so append /* to the path
|
||||
const dir_path = if (std.mem.eql(u8, entry.path, "/"))
|
||||
std.fmt.allocPrint(bun.default_allocator, "/*", .{}) catch bun.outOfMemory()
|
||||
else
|
||||
std.fmt.allocPrint(bun.default_allocator, "{s}/*", .{entry.path}) catch bun.outOfMemory();
|
||||
defer bun.default_allocator.free(dir_path);
|
||||
ServerConfig.applyStaticRoute(any_server, ssl_enabled, app, *DirectoryRoute, directory_route, dir_path, entry.method);
|
||||
},
|
||||
.html => |html_bundle_route| {
|
||||
ServerConfig.applyStaticRoute(any_server, ssl_enabled, app, *HTMLBundle.Route, html_bundle_route.data, entry.path, entry.method);
|
||||
|
||||
50
test/js/bun/http/directory-route-debug.test.ts
Normal file
50
test/js/bun/http/directory-route-debug.test.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { describe, it, expect, beforeAll, afterAll } from "bun:test";
|
||||
import { bunEnv, bunExe, tempDirWithFiles, isWindows } from "harness";
|
||||
import { join } from "path";
|
||||
import { readdirSync } from "fs";
|
||||
|
||||
describe("DirectoryRoute Debug", () => {
|
||||
let testDir: string;
|
||||
|
||||
beforeAll(() => {
|
||||
testDir = tempDirWithFiles("directory-route-debug", {
|
||||
"test.html": "<html><body>Test Content</body></html>",
|
||||
});
|
||||
console.log("Debug Test directory:", testDir);
|
||||
console.log("Files in directory:", readdirSync(testDir));
|
||||
});
|
||||
|
||||
it("should debug single file serving", async () => {
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
routes: {
|
||||
"/": { dir: testDir },
|
||||
},
|
||||
});
|
||||
|
||||
console.log("Server started on port:", server.port);
|
||||
console.log("Testing file at path: /test.html");
|
||||
|
||||
try {
|
||||
const response = await fetch(`http://localhost:${server.port}/test.html`);
|
||||
console.log("Response status:", response.status);
|
||||
console.log("Response headers:", Object.fromEntries(response.headers.entries()));
|
||||
|
||||
if (response.status !== 200) {
|
||||
const text = await response.text();
|
||||
console.log("Response body:", text);
|
||||
|
||||
// Also test if file exists directly
|
||||
const testFilePath = join(testDir, "test.html");
|
||||
console.log("Direct file check:", Bun.file(testFilePath).size);
|
||||
} else {
|
||||
const text = await response.text();
|
||||
console.log("Success! Response body:", text);
|
||||
}
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
} finally {
|
||||
server.stop();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user