Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

google app engine - Problem with deploying MERN stack app to GAE Standard Env

We're trying to deploy our MERN stack app to Google's App Engine. Before we were using flex environment, but to enable redirecting to https every time, we switched to Standard env. After this change, we encountered a few problems, such as: sometimes our main ('/') page doesn't load, for some users the website remains unsecured and:

Uncaught SyntaxError: Unexpected token '<'

What could be the solution to fix all of these problems?

Below I'm attaching code snippets.

Our code structure looks like:

  • client

    | - build

    | - src

    | - package.json

    | - ...

  • routes/api

  • package.json

  • server.js

  • app.yaml

  • ...

client/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

In our server.js we have:

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}`))
question from:https://stackoverflow.com/questions/65916608/problem-with-deploying-mern-stack-app-to-gae-standard-env

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...