好吧,StackOverflow中有很多关于此错误的问题,我已经看到所有这些都对我没有帮助。 我正在使用redux尝试获取一些产品。我的后端服务器节点正在端口5000上运行,并且每当我尝试使用前端运行端口3000来获取其数据时,我就会同时使用它同时启动两个服务器。请有人帮我解决这个问题
{
"name": "proshop",
"version": "1.0.0",
"description": "MERN application named proshop",
"main": "server.js",
"type": "module",
"scripts": {
"start": "node backend/server",
"server": "nodemon backend/server",
"client": "npm start --prefix frontend",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"data:import": "node backend/seeder",
"data:destroy": "node backend/seeder -d"
},
"keywords": [
"NodeJS",
"ExpressJs",
"React",
"Redux",
"MongoDb"
],
"author": "Narayan Maity",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"colors": "^1.4.0",
"concurrently": "^5.3.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-async-handler": "^1.1.4",
"mongoose": "^5.10.9"
},
"devDependencies": {
"nodemon": "^2.0.6"
}
}
import axios from 'axios';
import {
PRODUCT_LIST_FAIL,
PRODUCT_LIST_REQUEST,
PRODUCT_LIST_SUCCESS,
} from '../constants/productConstants';
export const listProducts = () => async (dispatch) => {
try {
dispatch({ type: PRODUCT_LIST_REQUEST });
const { data } = await axios.get('http://localhost:5000/api/products');
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
} catch (error) {
dispatch({
type: PRODUCT_LIST_FAIL,
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
});
}
};
答案 0 :(得分:1)
如果将focus
添加到"proxy": "http://localhost:5000"
或client
项目文件夹中,将更加容易,并且react
像这样:-
{
"name": "projectname",
"version": "0.1.0",
"private": true,
"dependencies": {
...}
"proxy": "http://localhost:5000"
}
那你就可以做:-
await axios.get('/api/products') // straight away use whatever path you've specified