node.js - 在 Google App Engine 上部署 create-react-app

标签 node.js reactjs google-app-engine google-cloud-platform create-react-app

我正在尝试部署使用 create-react-app 创建的应用程序在 Google App Engine 上。

我的应用程序在本地运行良好(npm startnpm run build & serve -s build )。 但是,在 Google App Engine 上部署的应用程序仅显示 index.html,并没有调用我的 React 代码。

Chrome 开发者控制台显示 Uncaught SyntaxError: Unexpected token < .

此外,我在 console.cloud.google.com 上发现部署的应用程序不包含构建文件夹。这是正确的吗?

谁能解决这个问题?

这是我的 package.json:

{
  "name": "XXXXX",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.8.2",
    "react-dom": "^16.8.2",
    "react-ga": "^2.5.7",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
 },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

此外,这是我的 app.yaml。

runtime: nodejs10

handlers:
- url: /.*
  static_files: build/index.html
  upload: build/index.html
- url: /
  static_dir: build

最佳答案

在网上搜索了很多次,发现是我的react app的路由系统导致的。

作为this post状态,我们需要小心创作 app.yaml。 如果我们先写下面的项目,GAE 返回所有查询的 build/index.html,包括访问 /static/js/static/css.

- url: /
  static_files: build/index.html
  upload: build/index.html

create-react-appnpm run build 创建一个构建文件夹和静态子文件夹。 因此,我必须像这样更具体地编写我的 app.yaml:

handlers:
  - url: /static/js/(.*)
    static_files: build/static/js/\1
    upload: build/static/js/(.*)
  - url: /static/css/(.*)
    static_files: build/static/css/\1
    upload: build/static/css/(.*)
  - url: /static/media/(.*)
    static_files: build/static/media/\1
    upload: build/static/media/(.*)
  - url: /(.*\.(json|ico))$
    static_files: build/\1
    upload: build/.*\.(json|ico)$
  - url: /
    static_files: build/index.html
    upload: build/index.html
  - url: /.*
    static_files: build/index.html
    upload: build/index.html

关于node.js - 在 Google App Engine 上部署 create-react-app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54820986/

相关文章:

javascript - Node 脚本永远不会结束

reactjs - React.memo 和 withRouter

java - 如何在 AppEngine Java 上使用 UserService 登录用户

performance - 使用命名空间 API 进行测试会影响我的 Google App Engine 应用程序的性能和可靠性吗?

node.js - 如何在 node.js 解析查询中设置 dns 服务器?

javascript - NPM 表示 UNMET_PEER_DEPENDENCY。为什么?

node.js - NodeJS类结构中的MongoDB

reactjs - Material UI 卡高度

javascript - React beautiful dnd 与 Material UI 列表中的问题

python - 在 python 中按日期分组的事件计数?