NestJS - 无法找到模块 'html'
NestJS - 无法找到模块 'html'
当我尝试渲染一个简单的静态HTML文件(没有模板)时,我遇到了一个错误。我不明白为什么会出现这个错误。我尝试了很多方法,但都没有成功。
src/modules/app.module.ts
import { Module } from '@nestjs/common' import { AppController } from './app.controller' import { AppService } from './app.service' import { AuthModule } from './auth/auth.module' import { ServeStaticModule } from '@nestjs/serve-static' import { join } from 'path' import { DiscordModule } from './bot/discord.module' import { UserModule } from './database/user/user.module' import { TypeOrmModule } from '@nestjs/typeorm' import { ConnectionOptions, getConnectionOptions } from 'typeorm' import { RoleModule } from './database/role/role.module' import { AppConfigModule } from './config/config.module' import { PassportModule } from '@nestjs/passport' @Module({ imports: [ AppConfigModule.forRoot({ isGlobal: true, cache: true, envFilePath: [ '.env.local', '.env' ], expandVariables: true, }), TypeOrmModule.forRootAsync({ useFactory: async () => Object.assign(await getConnectionOptions(),>{ autoLoadEntities: true, cache: true, }), }), UserModule, RoleModule, DiscordModule, AuthModule, PassportModule.register({ session: true }), ServeStaticModule.forRoot({ rootPath: join(__dirname, '..', '..', 'public'), exclude: [ '/api*' ], }), ], controllers: [ AppController ], providers: [ AppService ], }) export class AppModule {}
src/modules/app.controller.ts
import { Controller, Get, Render } from '@nestjs/common' import { AppService } from './app.service' @Controller() export class AppController { constructor(private readonly appService: AppService) {} @Get('/hello') getHello(): string { return this.appService.getHello() } @Get() @Render('index.html') root() {} }
src/main.ts
import { NestFactory, Reflector } from '@nestjs/core' import { ValidationPipe } from '@nestjs/common' import { AppModule } from './modules/app.module' import { NestExpressApplication } from '@nestjs/platform-express' import { AppConfigService } from './modules/config/config.service' import { AuthenticatedGuard } from './modules/auth/auth.guard' import session from 'express-session' import { TypeormStore } from 'connect-typeorm' import { getRepository } from 'typeorm' import { SessionEntity } from './database/entities/session.entity' import { SESSION_SECRET } from './modules/config/config.constants' import passport from 'passport' async function bootstrap() { const app = await NestFactory.create(AppModule) const config = app.get(AppConfigService) app.useGlobalPipes(new ValidationPipe()) app.useGlobalGuards(new AuthenticatedGuard(app.get(Reflector))) app.use(session({ cookie: { maxAge: 24 * 60 * 60 * 1000 }, secret: 'djsnjfjknskjfnanfkdfalsndjfndsj', resave: false, saveUninitialized: false, store: new TypeormStore().connect(getRepository(SessionEntity)), })) app.use(passport.initialize()) app.use(passport.session()) app.enableCors({ origin: [ config.baseUrl ], credentials: true, }) app.setViewEngine('html') await app.listen(config.port) } bootstrap()
Stacktrace
[Nest] 192424 - 2021-05-23, 6:07:20 p.m. [ExceptionsHandler] 找不到模块 'html' Require stack: - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/lib/view.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/lib/application.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/lib/express.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/index.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/platform-express/adapters/express-adapter.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/platform-express/adapters/index.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/platform-express/index.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/core/nest-factory.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/core/index.js - /home/drunkenponey/projects/the-4-horsemen/api/dist/main.js +3532ms Error: 找不到模块 'html' Require stack: - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/lib/view.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/lib/application.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/lib/express.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/index.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/platform-express/adapters/express-adapter.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/platform-express/adapters/index.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/platform-express/index.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/core/nest-factory.js - /home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/core/index.js - /home/drunkenponey/projects/the-4-horsemen/api/dist/main.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:941:15) at Function.Module._load (node:internal/modules/cjs/loader:774:27) at Module.require (node:internal/modules/cjs/loader:1013:19) at require (node:internal/modules/cjs/helpers:93:18) at new View (/home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/lib/view.js:81:14) at Function.render (/home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/lib/application.js:570:12) at ServerResponse.render (/home/drunkenponey/projects/the-4-horsemen/api/node_modules/express/lib/response.js:1012:7) at ExpressAdapter.render (/home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/platform-express/adapters/express-adapter.js:30:25) at RouterResponseController.render (/home/drunkenponey/projects/the-4-horsemen/api/node_modules/@nestjs/core/router/router-response-controller.js:27:36) at processTicksAndRejections (node:internal/process/task_queues:96:5)
问题的原因是使用了模板引擎而没有正确配置。解决方法是在不使用模板引擎的情况下使用sendFile
方法来发送静态文件。
在NestJS中,如果只是提供普通的HTML页面,不使用模板引擎,就不需要调用app.setVewEngine()
方法。只需要设置好静态目录即可。但是,如果希望用户在经过身份验证之前无法查看或下载这些静态文件,唯一的方法是从控制器的端点中渲染它(在这种情况下是app.controller.ts的root()
函数)。然而,如果不指定渲染引擎,会出现no default engine specified
错误。
然而,render
方法并不适用于不使用渲染引擎的情况。应该使用sendFile
方法来发送静态文件。
以下是解决该问题的正确代码示例:
import { Controller, Get, Res } from '@nestjs/common'; import { Response } from 'express'; @Controller() export class AppController { @Get() root(@Res() res: Response) { return res.sendFile('path/to/static/file.html'); } }
通过使用@Res()
装饰器来注入响应对象res
,然后使用sendFile
方法将静态文件发送给客户端。这样就可以在不使用模板引擎的情况下提供静态文件,并确保用户经过身份验证后才能访问。