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

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.

×
2 answers

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

Submit an Answer