如何在 Nest.js 中提供静态 HTML 文件?

Mau*_*ipf 12 javascript static-files node.js express nestjs

我想提供/dist位于 Nest 项目外部文件夹中的静态 HTML 文件。index.html已成功加载但无法加载任何 JS 文件(404错误)。

我有一个 Node/Express.js 项目,它使用

app.use('/', express.static('../client/dist'))
Run Code Online (Sandbox Code Playgroud)

它工作得很好。

然而,在 Nest 项目中,

app.setBaseViewsDir(join(__dirname, '../../client/dist'))
Run Code Online (Sandbox Code Playgroud)

不做的伎俩。

AppController我试过

import { Response } from 'express';

@Get()
  get(@Res() res: Response) {
    res.sendFile('index.html', {
      root: '../client/dist',
    });
  }
Run Code Online (Sandbox Code Playgroud)

但没有运气。

如前所述,index.html已成功加载。所以问题不是走错路。问题也不是 src-paths 错误,index.html因为在 Express 项目中使用了完全相同的文件。

/dist
  |-index.html
  |-main.js
  |-etc.
Run Code Online (Sandbox Code Playgroud)

在 index.html 中:

<script type="text/javascript" src="main.js"></script>
Run Code Online (Sandbox Code Playgroud)

当我将 dist 文件夹放入 Nest 项目(并调整路径)时,它也不起作用。

我找到了解决方案:

我现在使用 express 模块:

import * as express from 'express';
...
app.use('/', express.static('../client/dist'));
Run Code Online (Sandbox Code Playgroud)

Kim*_*ern 12

要提供静态文件,您必须使用useStaticAssets()而不是setBaseViewsDir()

app.useStaticAssets(join(__dirname, '../../client/dist'))
Run Code Online (Sandbox Code Playgroud)

使用时useStaticAssets无需设置控制器,您的所有文件都将自动提供:

假设client/dist您有文件index.htmlpic.jpg. 他们将担任:

localhost:3000 -> index.html
localhost:3000/pic.jpg -> pic.jpg
Run Code Online (Sandbox Code Playgroud)

当您想使用视图引擎时需要设置基本视图目录,例如hbs,请参阅文档

  • 请注意,您需要将 app 转换为正确的类型: `const app = wait NestFactory.create&lt;NestExpressApplication&gt;(AppModule);` 才能在打字稿中正确输入。 (5认同)

Phi*_*ief 9

关于Nest.js的官方文档应该提供这样的静态文件:

安装所需的包:

npm install --save @nestjs/serve-static
Run Code Online (Sandbox Code Playgroud)

app.module.ts更新为如下所示:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

@Module({
  imports: [
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'client'),   // <-- path to the static files
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)


小智 8

如果你有这样的事情

/public
  |-index.html
  |-main.js
  |-etc.
/src
  |-app.controller.js
  |-app.module.js
  |-main.js
Run Code Online (Sandbox Code Playgroud)

在 main.ts 或 main.js 中

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useStaticAssets(join(__dirname, '..', 'public'));
  app.setViewEngine('html');

  await app.listen(5000);
}
bootstrap();
Run Code Online (Sandbox Code Playgroud)

在app.module.js中

@Module({
  imports: 
  [ 
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'public'),
      exclude: ['/api*'],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

在 app.controller.js 中

@Controller()
@Dependencies(AppService)
export class AppController {
  constructor(appService) {
    this.appService = appService;
  }

  @Get()
  @Render('index')
  root() {
  }
}
Run Code Online (Sandbox Code Playgroud)

有了这段代码,你就可以做到这一点:) :) :)