React app error: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS

后端 未结 8 1575
南方客
南方客 2020-12-01 10:29

I am deploying a React app but am getting a strange error when I visit the page over https.

When I visit the page over https I receive the following error:

S

8条回答
  •  隐瞒了意图╮
    2020-12-01 10:58

    • Create in the back of the folder and node.js app
    • Create express router in the root of your app
    • Make a server.js file

    Add this code into server.js

    const express = require('express')
    const path = require('path')
    const app = express()
    
    if (process.env.NODE_ENV === 'production') {
      // Serve any static files
      app.use(express.static(path.join(__dirname, 'client/build')))
      // Handle React routing, return all requests to React app
      app.get('*', (request, response) => {
        response.sendFile(path.join(__dirname, 'client/build', 'index.html'))
      })
    }
    const port = process.env.PORT || 8080
    app.listen(port, () => {
      console.log(`API listening on port ${port}...`)
    })
    

    Into to pakage.json

    ,
      "scripts": {
        "start": "node server.js",
        "heroku-postbuild": "cd client && yarn && yarn run build"
      }
    

    And add the route proxy in to /folder-name-react-app/pakage.json

      "proxy": "http://localhost:8080"
    

提交回复
热议问题