Report this

What is the reason for this report?

Single page react app using node api gives 404 error on Production server

Posted on December 1, 2020

0

I have a validation form on React but on clicking submit m getting “404 Error”. It works perfect on local machine. //from app.js

handleSubmit = async e => { e.preventDefault(); const response = await fetch(‘/api/emailVerifier’, { method: ‘POST’, body: this.state, headers: { ‘Content-Type’: ‘application/json’, },

  //body: JSON.stringify({ post: this.state.post }),

});
const body = await response.text();

this.setState({ responseToPost: body });

}; //from server.js

app.post(‘/api/emailVerifier’, async (req, res) => { console.log(req.body); const emailValidator = new EmailValidator(); const { wellFormed, validDomain, validMailbox } = await emailValidator.verify(req.body.post); res.send(

`response received , welllformed = ${wellFormed}, validDomain = ${validDomain}, validMailbox = ${validMailbox}`,

); }); i have installed my my validator on server using npm i add deep-email-validator --save

My Directory on server has

asset-manifest.json index.html manifest.json package.json static build logo192.png node_modules robots.txt favicon.ico logo512.png package-lock.json server.js



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Have you wired your backend server correctly? From the looks of it, you are attempting to serve the client and server from the same host? If so, are you pointing the server to your build correctly? Here’s a reference from the create-react-app documentation

https://create-react-app.dev/docs/deployment#other-solutions

My hunch is that your client app is the only thing deployed and your server is not running on the same host or is not setup correctly. You want your server to power the client and send the build back on non-api paths as shown in the example at the link above.

Thanks my problem is fixed. I created separate project for react and node and tried deploying using app platform. It worked. Earlier I was having single project fro both of them

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.