1

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

3
  • 1
    what 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. Commented 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? Commented 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. Commented Jul 5, 2021 at 9:16

2 Answers 2

1

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

enter image description here

5.) Move all your server-related stuff into the root folder, that way, you don't have to cd into any nested folders.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello. Thanks for your help but now I receive an error "2021-07-05T09:58:54.585582+00:00 heroku[router]: at=info method=GET path="/" host=projectslist2.herokuapp.com request_id=ee27cab3-d6d0-4296-84ca-0863d3e8d820 fwd="88.1.81.122" dyno=web.1 connect=0ms service=2ms status=404 bytes=397 protocol=https" My new app address - projectslist2.herokuapp.com My new repo - github.com/VasVV/projects-list-2
1

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"
},

1 Comment

Thanks, but I recieve the same error as in previous answer - 2021-07-05T10:17:19.481777+00:00 heroku[router]: at=info method=GET path="/" host=projectslist.herokuapp.com request_id=85616105-a296-4281-8cef-095e9f948ed2 fwd="88.1.81.122" dyno=web.1 connect=1ms service=16ms status=404 bytes=397 protocol=https What can I do to fix it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.