bradtraversy / mern_shopping_list

Shopping List built with MERN and Redux
605 stars 436 forks source link

issue with proxy #29

Closed amahh014 closed 4 years ago

amahh014 commented 4 years ago

Hello everyone, I have an issue with proxy

] [HPM] Error occurred while trying to proxy request /api/items from localhost:3000 to http://localhost:5000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)

I can't add any item to my list, please help!

amahh014 commented 4 years ago

I found a solution, You need to add this line of code to server.js file,

app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
  });

also, you need to edit getItems in itemActions.js

I had to copy and paste

export const getItems= () => dispatch => {
    dispatch(setItemsLoading()); 
    axios
        .get('http://localhost:5000/api/items') <---I changed it to this from the original '/api/items/' because it was looking for it on port 3000
        .then(res => 
            dispatch({
                type: GET_ITEMS, 
                payload: res.data
            })
        )
};