温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/docs/runtime/file-system-router
点击访问原文链接

File System Router - Bun

File System Router - BunSkip to main contentBun home pageSearch...⌘KInstall BunSearch...NavigationFile & Module SystemFile System RouterRuntimePackage ManagerBundlerTest RunnerGuidesReferenceBlogFeedbackdiv:first-child]:!hidden peer-[.is-custom]:[&>div:first-child]:sm:!hidden peer-[.is-custom]:[&>div:first-child]:md:!hidden peer-[.is-custom]:[&>div:first-child]:lg:!hidden peer-[.is-custom]:[&>div:first-child]:xl:!hidden">Get StartedWelcome to BunInstallationQuickstartTypeScriptbun initbun createCore RuntimeBun RuntimeWatch ModeDebuggingREPLbunfig.tomlFile & Module SystemFile TypesModule ResolutionJSXAuto-installPluginsFile System RouterHTTP serverServerRoutingCookiesTLSError HandlingMetricsNetworkingFetchWebSocketsTCPUDPDNSData & StorageCookiesFile I/OStreamsBinary DataArchiveSQLSQLiteS3RedisConcurrencyWorkersProcess & SystemEnvironment VariablesShellSpawnInterop & ToolingNode-APIFFIC CompilerTranspilerUtilitiesSecretsConsoleYAMLMarkdownJSON5JSONLHTMLRewriterHashingGlobSemverColorUtilsStandards & CompatibilityGlobalsBun APIsWeb APIsNode.js CompatibilityContributingRoadmapBenchmarkingContributingBuilding WindowsBindgenLicenseOn this pageNext.js-styleReferenceFile & Module SystemFile System RouterCopy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_2shjinpfd9rqaabsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">*]:[overflow-wrap:anywhere]">Bun provides a fast API for resolving routes against file-system paths

Copy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_5hjinpfd9rqaabsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">This API is primarily intended for library authors. At the moment only Next.js-style file-system routing is supported, but other styles may be added in the future. ​Next.js-style The FileSystemRouter class can resolve routes against a pages directory. (The Next.js 13 app directory is not yet supported.) Consider the following pages directory: Copypages ├── index.tsx ├── settings.tsx ├── blog │   ├── [slug].tsx │   └── index.tsx └── [[...catchall]].tsx The FileSystemRouter can be used to resolve routes against this directory: router.tsCopyconst router = new Bun.FileSystemRouter({ style: "nextjs", dir: "./pages", origin: "https://mydomain.com", assetPrefix: "_next/static/" }); router.match("/"); // => { filePath: "/path/to/pages/index.tsx", kind: "exact", name: "/", pathname: "/", src: "https://mydomain.com/_next/static/pages/index.tsx" } Query parameters will be parsed and returned in the query property. Copyrouter.match("/settings?foo=bar"); // => { filePath: "/Users/colinmcd94/Documents/bun/fun/pages/settings.tsx", kind: "dynamic", name: "/settings", pathname: "/settings?foo=bar", src: "https://mydomain.com/_next/static/pages/settings.tsx", query: { foo: "bar" } } The router will automatically parse URL parameters and return them in the params property: Copyrouter.match("/blog/my-cool-post"); // => { filePath: "/Users/colinmcd94/Documents/bun/fun/pages/blog/[slug].tsx", kind: "dynamic", name: "/blog/[slug]", pathname: "/blog/my-cool-post", src: "https://mydomain.com/_next/static/pages/blog/[slug].tsx", params: { slug: "my-cool-post" } } The .match() method also accepts Request and Response objects. The url property will be used to resolve the route. Copyrouter.match(new Request("https://example.com/blog/my-cool-post")); The router will read the directory contents on initialization. To re-scan the files, use the .reload() method. Copyrouter.reload(); ​Reference Copyinterface Bun { class FileSystemRouter { constructor(params: { dir: string; style: "nextjs"; origin?: string; assetPrefix?: string; fileExtensions?: string[]; }); reload(): void; match(path: string | Request | Response): { filePath: string; kind: "exact" | "catch-all" | "optional-catch-all" | "dynamic"; name: string; pathname: string; src: string; params?: Recordstring, string>; query?: Recordstring, string>; } | null } }

Was this page helpful?

YesNoSuggest editsRaise issuePluginsPreviousServerNext⌘IxgithubdiscordyoutubePowered by

智能索引记录