【问题标题】:Is it possible to modify index.html before serving it using ServeStaticModule in NestJs?是否可以在使用 NestJs 中的 ServeStaticModule 服务之前修改 index.html?
【发布时间】:2026-01-05 22:15:01
【问题描述】:

我正在使用 ServeStaticModule 发回 index.html 文件。我想为 facebook 预览添加自定义元标记。我尝试在客户端这样做,但这不起作用,因为 Facebook 采用旧标题和描述,而不是在客户端修改的新标题和描述。客户端在 React 中。

有什么建议吗?

【问题讨论】:

    标签: javascript facebook nestjs meta


    【解决方案1】:

    你必须使用模板引擎 - read more

    模板示例

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <title>App</title>
      </head>
      <body>
        {{ message }}
      </body>
    </html>
    

    app.controller.ts

    import { Get, Controller, Render } from '@nestjs/common';
    
    @Controller()
    export class AppController {
      @Get()
      @Render('index')
      root() {
        return { message: 'Hello world!' };
      }
    }
    

    【讨论】:

    • 是的。它只是替换文件中的文本。
    最近更新 更多