NestJS - 找不到模块'html'
Posted
技术标签:
【中文标题】NestJS - 找不到模块\'html\'【英文标题】:NestJS - Cannot find module 'html'NestJS - 找不到模块'html' 【发布时间】:2021-08-12 08:08:54 【问题描述】:当我尝试渲染一个简单的静态 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(), <Partial<ConnectionOptions>>
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<NestExpressApplication>(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()
堆栈跟踪
[Nest] 192424 - 2021-05-23, 6:07:20 p.m. [ExceptionsHandler] Cannot find module '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: Cannot find module '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)
【问题讨论】:
【参考方案1】:如果您只是提供常规 HTML,而不使用模板引擎,则无需调用 app.setVewEngine()
。只需设置您的静态目录,您就可以开始使用了。 You can read more about this in this answer
【讨论】:
问题是我不希望用户能够在未经身份验证的情况下查看/下载这些静态文件,我发现确保用户通过身份验证的唯一方法是 render 它来自控制器的端点(在本例中为 app.controller.ts 的root()
函数)。但是,如果我没有指定渲染引擎,我会收到 no default engine specified
错误。
如果您不使用渲染引擎,则不应使用 render
。你应该使用sendFile
来发送一个静态文件。【参考方案2】:
尝试使用@Render('index')
而不是@Render('index.html')
【讨论】:
以上是关于NestJS - 找不到模块'html'的主要内容,如果未能解决你的问题,请参考以下文章
Nestjs 错误:找不到模块“./app.controller”
Nestjs错误。找不到模块'.app.controller'。
带有nestjs的Angular-universal:错误:找不到模块'./drivers/node-mongodb-native/connection'
NestJS - 测试套件无法运行从“comment/comment.entity.ts”中找不到模块“src/article/article.entity”