reactjs - React 应用程序未命中代理(package.json 文件)中描述的 url
问题描述
"proxy": "http://localhost:3001"
在我的 package.json 中,express 服务器在此端口( 3001 )上运行,但是每次我从 react 发出请求时,它都会在运行 react 应用程序的 3000 端口上运行
包.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"antd": "^3.10.2",
"axios": "^0.18.0",
"dotenv": "^6.1.0",
"http-proxy-middleware": "^0.19.0",
"material-ui": "^0.20.2",
"react": "^16.5.2",
"react-bootstrap": "^0.32.4",
"react-dom": "^16.5.2",
"react-form": "^3.5.6",
"react-redux": "^5.0.4",
"react-router-dom": "^4.3.1",
"react-router-redux": "^5.0.0-alpha.5",
"react-scripts": "2.0.5",
"styled-components": "^4.0.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
setupProxy.js
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(proxy('/api/*', { target: 'http://localhost:3001' }));
};
解决方案
"proxy": {
"/services": {
"target": "http://localhost:3001",
"changeOrigin": true,
"pathRewrite": {
"^/services": ""
}
}
}
npm 重新开始
推荐阅读
- php - Magento 2 安装设置 - 空白页
- ruby-on-rails - 向 DB 迁移添加列:条目未显示在 SHOW 视图中
- selenium-webdriver - 如何编写脚本来验证文本中的数字?例如:我喜欢从字段“状态(2)”中获取数值“2”
- swift - 核心数据对象被写入,但从未读取
- twilio - twilio 功能是否涵盖了 twilio 电话会议应用程序的基本用例?
- javascript - 来自 MVC.Grid 的模态视图(部分)
- jquery - 输入新搜索时无法清除搜索结果
- scala - Scala Map - 使用 map 函数替换键->值
- python-3.x - 无法标记 ec2 资源 boto3 python
- java - 在 .jar 导出中使用 .mid 文件