3

Using Oak, how can I serve HTML without the extension? e.g.
host:port/home.html -> host:port/home

Here's my current code to render my public/views folder:

router.get('/:page', async (ctx: Context, next: () => Promise<unknown>) => {
    await send(ctx, ctx.request.url.pathname, {
        root: join(Deno.cwd(), 'public', 'views'),
        extensions: ['htm', 'html']
    });

    await next();
});

The extensions option is not working or maybe I just use it the wrong way.

Edit

My fix is currently removing the .html extension (e.g. home.html -> home). Pretty sure there's a better way than this

Onyxzen
  • 41
  • 3

1 Answers1

1

You can use this to send the file:

router.get('/path', async (ctx:any) => {
    const text = await Deno.readTextFile('./file.html');
    ctx.response.headers.set("Content-Type", "text/html")
    ctx.response.body = text;
});
  • This worked for me. Thanks! One thing to note though, I had to also do this for any `src` references in the `file.html` as well – dmcd Aug 28 '23 at 02:01