问题
I have built a single-page weather app with React and Node.js but can't seem to get it to deploy to Heroku. So far, I have:
- Created a new app on Heroku called weather-app-react-node
- Logged into Heroku on the CLI
- Run the command 'heroku git:remote -a weather-app-react-node' in my terminal
- Added a Procfile with 'web: npm start' in it
- Ran 'git add .', 'git commit -m "Pushed to heroku"', 'git push heroku master'
My terminal tells me it is deployed and waiting but when I click on the link, I get this error message:
SecurityError: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.
I've tried to google it but can't seem to find anything relevant to my situation. Anyone know how to fix it?
heroku-site: https://weather-app-react-node.herokuapp.com/
github: https://github.com/caseycling/weather-app
回答1:
To deploy the React app to Heroku, I performed the following steps...
1. In your terminal, enter npm -v
and node -v
to get your npm and node version. In my case, my npm version is 6.14.1
& my node version is 12.13.0
.
2. In package.json
, add "main": "server.js",
and "engines": { "npm": "6.14.1", "node": "12.13.0" },
under the "private"
property. In your scripts
property, add "heroku-postbuild": "npm install"
and set "start"
to "node server.js"
.
3. In the root directory, create a Procfile
with one line of text: web: node server.js
.
4. In the root directory, create the server.js
file with the below code..
const express = require("express");
// eslint-disable-next-line no-unused-vars
// const bodyParser = require('body-parser');
const path = require("path");
const app = express();
const port = process.env.PORT || 8080;
app.use(express.static(path.join(__dirname, "build")));
// This route serves the React app
app.get('/', (req, res) => res.sendFile(path.resolve(__dirname, "build", "index.html")));
app.listen(port, () => console.log(`Server listening on port ${port}`));
5. Enter npm run build
in the terminal to produce the build
directory. Next, remove (or comment out) /build
from .gitignore
file (in root directory).
6. Test if server.js
works by entering node server.js
(or nodemon server.js
) in the terminal. If it works, server.js
should serve the React app.
7. Commit everything from step 1-6 to GitHub and Heroku repository. To commit to Heroku repository, in your terminal, enter heroku git:remote -a weather-app-react-node
and afterward, enter git push heroku master
.
回答2:
You can try logging in to heroku directly and deploy your github repository's desired branch from there directly.
回答3:
I used create-react-app-buildpack
npm install -g create-react-app
create-react-app my-app
cd my-app
git init
heroku create -b https://github.com/mars/create-react-app-buildpack.git
or
heroku create -b mars/create-react-app
git add .
git commit -m "I am the newborn app"
git push heroku master
heroku open
Note: In my case, buildpack config from CLI did not work, I still had nodejs-build pack, so I manually changed the build pack to mars/create-react-app
in the Heroku project dashboard
回答4:
The best practice to push React apps to Heroku with a node js backend is to use the Heroku Post Build Script, The post build will take care of all the work under the hood
Follow the steps below
Add This below snippet to your package.json under the scripts
scripts{
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix reactFolderName && npm run build --prefix reactFolderName"
}
And add this snippet to your index.js file
app = express()
app.use(express.static('reactFolderName/build'));
app.get('*', (req, res) => res.sendFile(path.resolve(__dirname, 'reactFolderName', 'build', 'index.html')));
来源:https://stackoverflow.com/questions/59850705/how-to-deploy-react-application-to-heroku