I was able to get a form working when using pug as the view engine with Express.js. But when I tried to use React, I have not been able to get form handling to work properly.
// Component.js
</React.Fragment>
// API_POST_URL=http://localhost:4000/api/donate
<form action={process.env.API_POST_URL} method='post' id='testForm'>
<input type="hidden" name='step' value='3' />
<button type='submit'>Post Test form</button>
</form>
</React.Fragment>
.
// index.js in another project folder
router.post('/api/donate', (req, res, next) => {
// code testing for earlier steps removed
else if (req.body.step === '3') {
res.json({ message : "here's a response back"});
}
});
The front end runs on port 3000, the back end on port 4000. Connecting the projects together incorrectly may be the issue. Routing is another possibility.
I have been able to receive a response object using Postman requests to the ENV variable URL above (http://localhost:4000/api/donate)