我们正在尝试将我们的 MERN 堆栈应用部署到 Google 的 App Engine。在我们使用 flex 环境之前,但为了启用每次重定向到 https,我们切换到 Standard env。进行此更改后,我们遇到了一些问题,例如:有时我们的主 ('/') 页面无法加载,对于某些用户而言,网站仍然不安全,并且:
Uncaught SyntaxError: Unexpected token '<'
解决所有这些问题的解决方案是什么?
下面我附上了代码片段。
我们的代码结构如下:
客户
| - 构建
| - 源代码
| - package.json
| - ...
路由/api
package.json
server.js
app.yaml
...
客户端/package.json
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:5000",
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
Package.json:
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client",
"test": "jest"
},
App.Yaml:
runtime: nodejs12
automatic_scaling:
max_instances: 2
resources:
cpu: .5
memory_gb: 0.5
disk_size_gb: 10
handlers:
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
- url: /
static_files: client/build/index.html
upload: client/build/index.html
secure: always
- url: /
static_dir: client/build
secure: always
在我们的 server.js 中,我们有:
if (process.env.NODE_ENV === 'production') {
console.log('production')
app.use(express.static(path.join(__dirname, '/client/build')))
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, '/client/build/index.html'))
})
}
const port = process.env.PORT || 5000
app.listen(port, () => console.log(`Server running on port ${port}`))