Last active
November 13, 2024 16:31
-
-
Save briward/878fc87cca9005a9fe7e2be54f7e910a to your computer and use it in GitHub Desktop.
Loading static files with Raptor Router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function template(name: string) { | |
const decoder = new TextDecoder('utf-8'); | |
const text = Deno.readFileSync( | |
`${Deno.cwd()}/templates/${name}.html` | |
); | |
return decoder.decode(text); | |
} | |
export default function asset(name: string, extension: string) { | |
const decoder = new TextDecoder('utf-8'); | |
const text = Deno.readFileSync( | |
`${Deno.cwd()}/public/${name}.${extension}` | |
); | |
return decoder.decode(text); | |
} | |
const router = new Router(); | |
router.add(new Route({ | |
name: 'static', | |
pathname: `/public/:path+.css`, | |
method: HttpMethod.GET, | |
handler: () => asset('style', 'css') | |
})); | |
router.add(new Route({ | |
name: 'index', | |
pathname: '/', | |
method: HttpMethod.GET, | |
handler: () => template('index') | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment