Dai*_*ATO 3 google-app-engine node.js reactjs google-cloud-platform create-react-app
我正在尝试在Google App Engine上部署使用create-react-app创建的应用程序。
我的应用程序在本地(npm start和npm 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"
]
}
Run Code Online (Sandbox Code Playgroud)
另外,这是我的app.yaml。
runtime: nodejs10
handlers:
- url: /.*
static_files: build/index.html
upload: build/index.html
- url: /
static_dir: build
Run Code Online (Sandbox Code Playgroud)
您可以检查这份文件吗?-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
Run Code Online (Sandbox Code Playgroud)
谢谢!
经过多次搜索,我发现我的应用程序的路由系统是原因。
如这篇文章所述,我们在编写app.yaml时需要小心。如果我们首先编写以下项目,则GAE将返回build/index.html所有查询,包括对/static/js和的访问/static/css。
- url: /
static_files: build/index.html
upload: build/index.html
Run Code Online (Sandbox Code Playgroud)
create-react-app为创建一个构建文件夹和静态子文件夹npm 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3945 次 |
| 最近记录: |