I am getting this exact error when trying to send a POST request to log in a user:
[HPM] Error occurred while trying to proxy request /api/login from localhost:3000 to http://localhost:5000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
At first i thought this was an issue relating to how the proxy was set up between the client and the server but I can fetch data from the backend perfectly fine. So the problem has to do when I am trying to post data to the backend (I think??)
Here is my React Login component:
import React from 'react'
class Login extends React.Component {
state = {}
handlChange = event => {
const { name, value } = event.target
this.setState({ [name]: value })
}
handleSubmit = () => {
fetch("/api/login", {
method: "POST",
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(this.state)
})
}
render() {
console.log(this.state)
return (
<div>
<form onSubmit={this.handleSubmit}>
<input type="text" name="username" value={this.state.username} onChange={this.handlChange} placeholder="Username" />
<input type="password" name="password" value={this.state.password} onChange={this.handlChange} placeholder="Password" />
<button>Login</button>
</form>
</div>
)
}
}
export default Login
I am using a setUpProxy.js file within the src folder as suggested by the React docs to set up the proxy, it looks like this:
const proxy = require("http-proxy-middleware")
module.exports = function(app) {
app.use(
'/api',
proxy({
target: 'http://localhost:5000',
changeOrigin: true
})
);
};
The client side app was built with create-react-app if that makes any difference