首页 > 解决方案 > 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' }));
};

标签: reactjsexpress

解决方案


"proxy": {
"/services": {
  "target": "http://localhost:3001",
  "changeOrigin": true,
  "pathRewrite": {
    "^/services": ""
  }
}

}

npm 重新开始


推荐阅读