【问题标题】:Using oak, serving HTML without extension? (Deno)使用 Oak,无需扩展即可提供 HTML? (德诺)
【发布时间】:2021-09-04 07:50:17
【问题描述】:

使用Oak,如何在没有扩展的情况下提供 HTML?例如
host:port/home.html -> host:port/home

这是我当前的代码来呈现我的public/views 文件夹:

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();
});

extensions 选项不起作用,或者我只是使用了错误的方式。

编辑

我的修复目前正在删除 .html 扩展名(例如 home.html -> home)。肯定有比这更好的方法

【问题讨论】:

    标签: javascript deno oak


    【解决方案1】:

    您可以使用它来发送文件:

    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;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 2019-09-17
      • 2016-01-02
      相关资源
      最近更新 更多