我正在使用App Engine托管Nodejs Spa,我不需要路由,因此我想将所有处理程序重定向到Root One。示例:将domain.tld/nsa-secrets
重定向到domain.tld
我尝试将处理程序配置为文档中的示例,但是我总是找不到默认的apache html。
这是我的app.yaml
:
runtime: nodejs10
instance_class: F2
handlers:
- url: /
static_files: dist/index.html
upload: dist/index.html
- url: /
static_dir: dist
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
error_handlers:
- file: error.html
我看到错误然后在错误报告中显示:
Error: Cannot find module '/srv/server.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
at Function.Module._load (internal/modules/cjs/loader.js:508:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
我需要在server.js
中手动实现URL处理程序?
感谢您的时间和帮助。
您会尝试Firebase Hosting吗?
Firebase托管比App Engine更好地托管Spa。
请参阅:
- https://firebase.google.com/docs/hosting/use-case
受益于Firebase Hosting的独特优化,用于服务单页网络应用程序和静态网站。静态资产(HTML,CSS,JavaScript,字体等(的交付由我们的SSD后端存储和全球所有主要位置的全球CDN提供动力。您甚至可以在全局CDN上缓存您的动态内容。Firebase托管的所有站点也获得免费的SSL证书,因此您的内容始终可以安全地交付。
- https://firebase.google.com/docs/hosting/full-config#rewrites
firebase firebase托管 nestjs接近的角 云功能。
- 将
index.html
重命名为index2.html
。这对于渲染路线路径很重要,否则您将在所有路线上渲染正常工作,不包括根/
。 - 更新
angular.json
具有以下"index": "apps/myapp/src/index2.html",
(只需将index.html
更改为index2.html
(即可。注意:index.html的路径对您来说可能有所不同,我正在使用NX Workspace 。 - 将
templatePath: join(BROWSER_DIR, 'index2.html'),
添加到Nestjs的ApplicationModule
中,很可能您将文件命名为 app.module.ts 在 Server 目录中。
喜欢:
@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
})
]
})
初始化Firebase Cloud功能和Firebase Hosting,有关如何设置此设置您可以检查https://hackernoon.com/deploying-angular-universal-universal-universal-v6-with-firebase-firebase-c86381ddddd445或https或https://博客://博客。
编辑您的 firebase.json 。
它应该看起来像那样,或者至少是hosting
部分。
{
"hosting": {
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"public": "functions/path/to/browser",
"rewrites": [
{
"function": "angularUniversalFunction",
"source": "**"
}
]
}
}
- 在您的 main.ts 中,您需要在服务器上设置云功能。
在极简主义的情况下,它想要类似的东西:
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<NestExpressApplication>(
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注意路由。您可能注意到,这是服务器端渲染(SSR(,但是可以使用Nestjs Cloud函数将所有路由重定向到index.html
(或更精确的index2.html
(。另外,您有一个SSR"免费":(
要展示的项目:
1(Angular Angular Universal(SSR( firebase的云功能:https://github.com/ismaestro/angular8-example-app-(缺少Nestjs(。
2(Angular Nestjs:https://github.com/kamilmysliwiec/universal-nest(Firebase缺少云功能(。