您只能使用
setbaseViewsDir并
@Render()与像车把(HBS)视图引擎;
用于提供静态文件(角度),但是,您只能使用
useStaticAssets和
response.sendFile。
要使用
index.html其他所有路线,您有两种可能:
A)中间件
您可以创建执行重定向的中间件,请参阅本文:
@Middleware()export class FrontendMiddleware implements NestMiddleware { resolve(...args: any[]): ExpressMiddleware { return (req, res, next) => { res.sendFile(path.resolve('../frontend/dist/my-app/index.html'))); }; }}然后为所有路由注册中间件:
export class ApplicationModule implements NestModule { configure(consumer: MiddlewaresConsumer): void { consumer.apply(FrontendMiddleware).forRoutes( { path: '/**', // For all routes method: RequestMethod.ALL, // For all methods }, ); }}B)全局错误过滤器
您可以将所有重定向
NotFoundExceptions到
index.html:
@Catch(NotFoundException)export class NotFoundExceptionFilter implements ExceptionFilter { catch(exception: HttpException, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); response.sendFile(path.resolve('../frontend/dist/my-app/index.html'))); }}然后在您的中将其注册为全局过滤器
main.ts:
app.useGlobalFilters(new NotFoundExceptionFilter());



