您只能使用setBaseViewsDir
并@Render()
与像车把(HBS)视图引擎; 用于提供静态文件(角度),但是,您只能使用useStaticAssets
和response.sendFile
。
要使用index.html
其他所有路线,您有两种可能:
@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
},
);
}
}
您可以将所有重定向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());