为什么不反应代理请求

时间:2018-07-23 03:55:50

标签: javascript node.js reactjs server proxy

我有一个运行在端口3001上的节点后端服务器,以及一个我试图代理到该端口以获取数据的反应服务器。

现在,我已经在我的客户端文件夹的package.json文件中添加了“代理”行,以告知React将请求代理到端口3001。

但是,当我尝试向服务器上的“测试”路由发出简单请求时,React代码不会代理该请求。它会将我发送到localhost:3000 / test而不是localhost:3001 / test。

我不确定为什么会这样。 为什么React代码无法正确代理我的请求?

Package.json(客户端)

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:3001"
}

Package.json(节点)

{
  "name": "todo-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha tests/*.test.js  --exit",
    "test-watch": "export NODE_ENV=test || \"SET NODE_ENV=test\" && nodemon --exec \"npm test\"",
    "dev-server": "node server.js",
    "client": "npm run start --prefix client",
    "dev": "concurrently \"npm run dev-server\" \"npm run client\""
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.18.0",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "concurrently": "^3.6.0",
    "express": "^4.16.3",
    "jsonwebtoken": "^8.2.2",
    "lodash": "^4.17.1i0",
    "mongodb": "^3.1.0-beta4",
    "mongoose": "^5.1.1",
    "nodemailer": "^4.6.4",
    "twitter-validate": "^1.0.8",
    "validator": "^10.3.0"
  },
  "devDependencies": {
    "expect": "^1.20.2",
    "mocha": "^5.1.1",
    "nodemon": "^1.17.4",
    "rewire": "^4.0.1",
    "supertest": "^3.1.0"
  }
}

登录路线:

  login(e) {
    e.preventDefault();
    axios.get("/test")
    .then((res) => {
      console.log(res);
    })
    .catch((e) => {
      console.log(e);
    })
  }

还有实际的index.js文件...

import React from "react";
import ReactDOM from "react-dom";
import "./styles/styles.scss";
import axios from "axios";

  class Login extends React.Component {

  showRoute(e){
    e.preventDefault();
    axios.get("/test")
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.log(err);
      });
  }

  render(){
    return (
      <div>
        <form onSubmit={this.login}>
          <legend>Login</legend>
          <label>Email:</label>
          <input type='text' name='email'/>
          <label>Password:</label>
          <input type='text' name='password'/>
          <button>Login</button>
        </form>
        <button onClick={this.showRoute}>Forgot Password?</button>
      </div>
    );
  }
}

ReactDOM.render(<Login />, document.getElementById("app"));

我得到的错误消息是:

GET http://localhost:3000/test 404 (Not Found)

2 个答案:

答案 0 :(得分:0)

您误解了代理服务器。
代理不会更改您请求的URL,它只是将请求从localhost:3000传输到localhost:3001。
程序无法正常运行的问题可能是由于配置代理的方式错误引起的。作为您发布的代码,可以在server.js中设置代理。

答案 1 :(得分:0)

问题在webpack配置中,将webpack.dev.js设置为波纹管,然后重新启动开发服务器

module.exports = merge(common, { mode: 'development', devServer: { host: 'localhost', port: 3000, open: true, historyApiFallback: true, proxy: { '/api': { target: 'http://localhost:3001', secure: false } } }, devtool: 'inline-source-maps' });

如果您将自己的入门包用于React,则此解决方案有效,在这种情况下,请尝试找出“ create-react-app”的webpack开发配置文件在哪里,并按上面的代码进行更改。应该在路径上“ / node_modules / react-scripts / config”。

干杯!