0
[HPM] Error occurred while trying to proxy request /api/users/ from localhost:3000 to http://localhost:5000 (ECONNRESET)

I get this error while trying to do axios.post request, i'm running my app with concurrently (server with express and client side using react) ,

as described here I defined client/src/setupProxy.js file:

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
    app.use(
        '/api',
        createProxyMiddleware({
            target: 'http://localhost:5000',
            changeOrigin: true,
        })
    );
};

my server.js:

const express = require('express');
const connectDB = require('./config/db');

const app = express();

//connect Database
connectDB();

//Init MiddleWare -allows to get the data from req.body
app.use(express.json());

//Define routes
app.use("/api/users", require("./routes/api/users"));
app.use("/api/auth", require("./routes/api/auth"));
app.use("/api/profile", require("./routes/api/profile"));
app.use("/api/posts", require("./routes/api/posts"));


app.get('/', (req, res) => res.send('API is running'));

//Port - first look for an environment port (when connect to heroku)
const PORT = process.env.PORT || 5000;


app.listen(PORT, () => {
    console.log(`server listening on port: ${PORT}`);
})

and my post request in the client side:

export const register = ({ name, email, password}) => async dispatch => {
    const config = {
        headers: {
            'Content-Type': 'application/json'
        }
    }

    const body = JSON.stringify({name, email, password});

    try {
        const res = await axios.post('/api/users/', body, config);

        console.log({res});

        dispatch({
            type: REGISTER_SUCCESS,
            payload: res.data
        })

        console.log('passed dispatch')
    } catch (e) {
        const errors = e.response.data.errors;

        if(errors) {
            errors.forEach(e => dispatch(setAlert(e.msg, 'danger')));
        }
        dispatch({
            type: REGISTER_FAIL
        })
    }
}

any idea what I miss ?

EDIT:

I've noticed that my server using type ipv6 and client on ipv4, maybe that's the problem ? how I make them run on the same ipv ?

Guy Ben
  • 1
  • 2
  • Why are you sending stringified body `const body = JSON.stringify({name, email, password});`? You should send the object in axios.post. – Boris Kukec Jan 30 '21 at 18:01
  • @Boris what does it matter? I parse the body in the server side, also tried without stringify and still getting the proxy problem – Guy Ben Jan 30 '21 at 19:35

0 Answers0