在Google App Engine上部署create-react-app

时间:2019-02-22 06:05:16

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

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

我的应用程序在本地(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

3 个答案:

答案 0 :(得分:4)

您可以检查该文档-https://medium.com/tech-tajawal/deploying-react-app-to-google-app-engine-a6ea0d5af132

这将为您提供帮助。我已经按照相同的步骤部署了我的应用。

示例yaml文件

    runtime: python27
    api_version: 1
    threadsafe: true
    handlers:
    - url: /
      static_files: build/index.html
      upload: build/index.html
    - url: /
      static_dir: build

参考链接-https://medium.com/google-cloud/how-to-deploy-a-static-react-site-to-google-cloud-platform-55ff0bd0f509

谢谢!

答案 1 :(得分:3)

由于多次搜索网络,我发现我的应用程序的路由系统是原因。

正如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

答案 2 :(得分:-1)

这完全适合我。

runtime: nodejs10 service: bbi-staging env: standard instance_class: F2 handlers: - url: /(.*\.(gif|media|json|ico|eot|ttf|woff|woff2|png|jpg|css|js))$ static_files: build/\1 upload: build/(.*) - url: /(.*) static_files: build/index.html upload: build/index.html