栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将所有路由重定向到nest.js中的index.html(角)?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将所有路由重定向到nest.js中的index.html(角)?

您只能使用

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());


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/413277.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号