是否可以在 nodejs 应用程序中将所有 url 路径重定向到 App Engine 中的根处理程序? [英] Is it possible to redirect all url paths to the root handler in App Engine in a nodejs app?

查看:20
本文介绍了是否可以在 nodejs 应用程序中将所有 url 路径重定向到 App Engine 中的根处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 App Engine 来托管 Nodejs SPA,我不需要路由,所以我想将所有处理程序重定向到根处理程序.示例:将 domain.tld/nsa-secrets 重定向到 domain.tld

我已尝试将处理程序配置为文档中的示例,但我总是得到默认的 not found Apache html.

这是我的app.yaml:

运行时:nodejs10实例类:F2处理程序:- 网址:/静态文件:dist/index.html上传:dist/index.html- 网址:/静态目录:dist- 网址:/.*安全:始终redirect_http_response_code: 301脚本:自动错误处理程序:- 文件:error.html

我看到一个错误,然后在错误报告中显示:

错误:找不到模块/srv/server.js"在 Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)在 Function.Module._load (internal/modules/cjs/loader.js:508:25)在 Function.Module.runMain (internal/modules/cjs/loader.js:754:12)启动时(内部/bootstrap/node.js:283:19)在 bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

我是否需要在 server.js 中手动实现一个 url 处理程序?

感谢您的时间和帮助.

解决方案

您会尝试 Firebase 托管吗?

Firebase 托管比 App Engine 更适合托管 SPA.

见:

I'm using App Engine to host a Nodejs SPA, I don't need routes so I would like to redirect all handlers to the root one. Example: redirect domain.tld/nsa-secrets to domain.tld

I have tried configuring a handler as the example in the documentation, but I always get the default not found Apache html.

Here is my 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

I see an error then shows in Error Reporting:

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)

Shall I need to manually implement an url handler in server.js?

Thanks for the time and help.

解决方案

Would you try Firebase Hosting?

Firebase Hosting is better than App Engine to hosting a SPA .

See:

  • https://firebase.google.com/docs/hosting/use-cases

    Benefit from Firebase Hosting's unique optimization for serving single-page web apps and static websites. Delivery of static assets (HTML, CSS, JavaScript, fonts, etc.) is powered by our SSD backend storage and a global CDN with edge locations across all major locations in the world. You can even cache your dynamic content on the global CDN. All sites hosted by Firebase also get a free SSL certificate, so your content is always delivered securely.

  • https://firebase.google.com/docs/hosting/full-config#rewrites

这篇关于是否可以在 nodejs 应用程序中将所有 url 路径重定向到 App Engine 中的根处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆