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

您还可以将Cloud Functions for Firebase与Firebase Hosting一起使用。您在main.ts中拥有的一切都很好,使用这种方法,您甚至不需要控制器。您应该执行以下操作:

将index.html重命名为index2.html。这对于渲染路径很重要,否则,除了根/以外,所有路径上的渲染效果都很好。

更新angular.json使其具有以下"index": "apps/myapp/src/index2.html",(将index.html更改为index2.html)。 注意:index.html的路径可能与您不同,我正在使用Nx工作区。

将templatePath: join(BROWSER_DIR, 'index2.html'),添加到NestJS的ApplicationModule中,很可能是在服务器目录中将文件命名为app.module.ts。

像这样:

@Module({

imports: [

AngularUniversalModule.forRoot({

bundle: require('./path/to/server/main'), // Bundle is created dynamically during build process.

liveReload: true,

templatePath: join(BROWSER_DIR, 'index2.html'),

viewsPath: BROWSER_DIR

})

]

})

应该看起来像这样,或者至少是hosting部分。

{

"hosting": {

"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],

"public": "functions/dist/apps/path/to/browser",

"rewrites": [

{

"function": "angularUniversalFunction",

"source": "**"

}

]

}

}

在您的main.ts中,您需要在服务器上设置Cloud Functions。

在极简主义的情况下,它会像这样:

import * as admin from 'firebase-admin';

import * as functions from 'firebase-functions';

admin.initializeApp(); // Initialize Firebase SDK.

const expressApp: Express = express(); // Create Express instance.

// Create and init NestJS application based on Express instance.

(async () => {

const nestApp = await NestFactory.create(

ApplicationModule,

new ExpressAdapter(expressApp)

);

nestApp.init();

})().catch(err => console.error(err));

// Firebase Cloud Function for Server Side Rendering (SSR).

exports.angularUniversalFunction = functions.https.onRequest(expressApp);

使用这种方法,您不必关心NestJS方面的路由。您可以在Angular端设置所有内容,仅此而已。 Angular负责路由。您可能已经注意到,这是服务器端渲染(SSR),但是可以结合使用用于Firebase的NestJS + Cloud Functions将所有路由重定向到index.html(或更准确地说是index2.html)。另外,您还可以免费获得SSR:)

展示项目:

1)Firebase的Angular + Angular Universal(SSR)+云功能:https://github.com/Ismaestro/angular8-example-app(缺少NestJS)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值