예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
정적 패턴, :param, *rest 패턴과, 부모 두 곳에 마운트한 섹션 테이블입니다. 경로를 입력하고, 아무것도 불러오지 않은 채 결과를 돌려주는 match()를 지켜보세요.
nuclo-router는 nuclo에 포함되어 있지 않으므로 따로 설치해야 합니다:npm install nuclo nuclo-routerStatic patterns win over dynamic ones, so /blog/new is not a slug. The section table is mounted under two parents with no path written twice.
// npm install nuclo nuclo-router import { createRouter, type RouteTable } from 'nuclo-router' // A reusable section: "./" patterns are relative to the parent. const recordSection: RouteTable = { '/': () => import('./Record.ts'), // the parent path itself './preview': () => import('./Preview.ts'), './preview/raw': () => import('./Raw.ts'), } const router = createRouter({ '/': () => import('./pages/Home.ts'), '/blog/new': () => import('./pages/NewPost.ts'), '/blog/:slug': () => import('./pages/Post.ts'), '/invoices/:id': recordSection, '/customers/:id': recordSection, '/files/*rest': () => import('./pages/File.ts'), '*': () => import('./pages/NotFound.ts'), }) // match() resolves a URL and loads nothing: no import, no history. router.match('/blog/new') // { pattern: '/blog/new', params: {} } static wins router.match('/blog/hello') // { pattern: '/blog/:slug', params: { slug: 'hello' } } router.match('/customers/7/preview/raw') // { pattern: '/customers/:id/preview/raw', params: { id: '7' } } router.match('/files/a/b.txt') // { pattern: '/files/*rest', params: { rest: 'a/b.txt' } } // On a server, a 404 before importing a single chunk: if (!router.match(request.url)) { return new Response(null, { status: 404 }) }