React + Node / Express + Mongo App-无法部署在Google App Engine中

时间:2019-03-31 15:13:12

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

我的这个应用程序在localhost的本地计算机上运行良好。但是无法部署在Google App Engine中。我已经使用app.yaml文件尝试了许多试验,但是无法成功部署它。 很可能是我错误地配置了app.yaml文件和package.json文件。

这是 full source code of the project

这是项目根目录(对于服务器)中package.json文件的关键部分。

    "scripts": {
        "server": "nodemon index.js",
        "client": "npm start --prefix client",
        "dev": "concurrently \"npm run server\" \"npm run client\"",
        "start": "cd client && npm install && npm run build && cd .. && npm run dev",
        "heroku-postbuild": "cd client && npm install && npm run build",
        "fix-code": "prettier-eslint --write 'src/**/*.{js,jsx}' ",
        "fix-styles": "prettier-stylelint --write 'src/**/*.{css,scss}' "
    },

在我的客户端目录(为我的React客户端提供服务)中,我有package.json for React

下面是我在项目根目录下的app.yaml code

runtime: nodejs
env: flex

handlers:
  - url: /api/.*
    script: auto
  - url: /
    static_files: client/build/index.html
    upload: client/build/index.html
  - url: /
    static_dir: client/build

当我最终运行gcloud app deploy时,我得到了错误提示

**ERROR: (gcloud.app.deploy) Error Response: [9] 
Application startup error:
> server@1.0.0 start /app
> cd client && npm install && npm run build && cd .. && npm run dev**


> jss@9.8.7 postinstall /app/client/node_modules/jss
> node -e "console.log('\u001b[35m\u001b[1mLove JSS? You can now support us on open collective:\u001b[22m\u001b[39m\n > \u001b[34mhttps://opencollective.com/jss/donate\u001b[0m')"


npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! server@1.0.0 start: `cd client && npm install && npm run build && cd .. && npm run dev`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-03-31T14_50_44_347Z-debug.log

要从gcloud获取日志,我运行gcloud app logs tail -s default,它显示以下内容

2019-03-31 13:28:18 default[20190331t185321]  Express Server running on port 8080
2019-03-31 13:28:19 default[20190331t185321]  Connected to Database (MongoDB)

2019-03-31 13:28:19 default[20190331t185321]  (node:18) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
2019-03-31 13:36:18 default[20190331t185321]  ::ffff:172.17.0.4 - - [Sun, 31 Mar 2019 13:36:18 GMT] "GET / HTTP/1.1" 404 139 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
2019-03-31 13:36:18 default[20190331t185321]  ::ffff:172.17.0.4 - - [31/Mar/2019:13:36:18 +0000] "GET / HTTP/1.1" 404 139
2019-03-31 14:49:41 default[20190331t201413]
2019-03-31 14:49:41 default[20190331t201413]  > server@1.0.0 start /app
2019-03-31 14:49:41 default[20190331t201413]  > cd client && npm install && npm run build && cd .. && npm run dev
2019-03-31 14:49:41 default[20190331t201413]
2019-03-31 14:49:42 default[20190331t201413]
2019-03-31 14:49:42 default[20190331t201413]  > server@1.0.0 start /app
2019-03-31 14:49:42 default[20190331t201413]  > cd client && npm install && npm run build && cd .. && npm run dev
2019-03-31 14:49:42 default[20190331t201413]
2019-03-31 14:50:42 default[20190331t201413]
2019-03-31 14:50:42 default[20190331t201413]  > jss@9.8.7 postinstall /app/client/node_modules/jss
2019-03-31 14:50:42 default[20190331t201413]  > node -e "console.log('\u001b[35m\u001b[1mLove JSS? You can now support us on open collective:\u001b[22m\u001b[39m\n > \u001b[34mhttps://opencollective.com/jss/donate\u001b[0m')"
2019-03-31 14:50:42 default[20190331t201413]
2019-03-31 14:50:43 default[20190331t201413]
2019-03-31 14:50:43 default[20190331t201413]  npm ERR! code ELIFECYCLE
2019-03-31 14:50:43 default[20190331t201413]  npm ERR! errno 1
2019-03-31 14:50:43 default[20190331t201413]  npm ERR! server@1.0.0 start: `cd client && npm install && npm run build && cd .. && npm run dev`
2019-03-31 14:50:43 default[20190331t201413]  npm ERR! Exit status 1
2019-03-31 14:50:43 default[20190331t201413]  npm ERR!
2019-03-31 14:50:43 default[20190331t201413]  npm ERR! Failed at the server@1.0.0 start script.

1 个答案:

答案 0 :(得分:1)

Google AppEngine上的节点JS应用以npm run start开头。您需要使“启动”过程保持简单(请参见文档)。您正在“开始”脚本中更改文件夹,但是在AppEngine上没有这些文件夹。只要确保您的“开始”脚本很简单即可。