I have deployed my simple React app to Heroku - https://projectslist.herokuapp.com It works just fine except for the server part which doesn't work at all. I've seen the logs and it says that my only server method - /sendmail - returns error 404. How can i fix this problem? I suppose that I should edit something in package.json file but I'm not sure what I should change. Here's my github repo - https://github.com/VasVV/Projects-list
-
1what is ur Procfile? also, in the package.json u have no script that runs the server how u are making sure that the server is indeed running? the create-react-app starts a webpack-dev-server i think. bundle ur react code and use express.static to host that maybe.Aritra Chakraborty– Aritra Chakraborty2021-07-05 08:43:21 +00:00Commented Jul 5, 2021 at 8:43
-
@AritraChakraborty I don't have a Procfile. As for script if I edit my package.json as follows: "start": "react-scripts start && node server" will it work? And what do u mean by bundling my code and using express.static to host?Алексей Николаев– Алексей Николаев2021-07-05 08:50:03 +00:00Commented Jul 5, 2021 at 8:50
-
Perhaps having a different Heroku app for your server will solve your problem. Make sure to update your API URLs.Albab– Albab2021-07-05 09:16:18 +00:00Commented Jul 5, 2021 at 9:16
2 Answers
Try putting your server files in the root directory folder and everything else in a client folder within the root:
1.) Put your server.js into the root directory
2.) Add these commands to your root 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"
},
"proxy": "http://localhost:4242/"
3.) Create a client folder and put your src and public folders in it.
4.) Add these commands to your client folders package.json...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Your file structure from root should look like this:
Root
client
Public
Src
package.json
server.js
package.json
5.) Move all your server-related stuff into the root folder, that way, you don't have to cd into any nested folders.
1 Comment
Add a Procfile to your root folder that says something like:
web: node server/index.js -port 8000
And a small addition to the scripts part of your package.json that tells Heroku that once it's fetched all the dependencies for your project it should build it, although that may not be necessary now.
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"heroku-postbuild": "npm run build"
},
