使用axios时遇到代理问题的麻烦

时间:2019-04-06 22:32:34

标签: node.js reactjs

我遇到一个奇怪的问题,我在package.json文件中添加了代理值,但是当我尝试使用axios与服务器连接时,我发现我的应用程序定向到端口3000(这是默认路径)用于React应用。

我的node.js服务器在端口5000上运行。

我正在使用“并发”包来帮助运行nodejs并同时做出反应。

static void swap(Container panel,
                 int firstIndex,
                 int secondIndex) {

    if (firstIndex == secondIndex) {
        return;
    }

    if (firstIndex > secondIndex) {
        int temp = firstIndex;
        firstIndex = secondIndex;
        secondIndex = temp;
    }

    Component first = panel.getComponent(firstIndex);
    Component second = panel.getComponent(secondIndex);

    panel.remove(first);
    panel.remove(second);

    panel.add(second, firstIndex);
    panel.add(first, secondIndex);
}
{
  "scripts": {
    "client-install": "npm install --prefix client",
    "start": "node server.js",
    "server": "nodemon server.js",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\""
  },
"dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "concurrently": "^4.1.0",
    "express": "^4.16.4",
    "gravatar": "^1.8.0",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.4.21",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "validator": "^10.11.0"
  },
  "devDependencies": {
    "nodemon": "^1.18.10"
  }
}
// Use Routes
app.use('/api/users', users);
app.use('/api/profile', profile);
app.use('/api/posts', posts);

const port = process.env.PORT || 5000;

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.18.0",
    "classnames": "^2.2.6",
    "jwt-decode": "^2.2.0",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-redux": "^6.0.1",
    "react-router-dom": "^5.0.0",
    "react-scripts": "2.1.8",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000",
}

我希望请求网址应为“ http://localhost:5000/api/profile”,但我得到了“ http://localhost:3000/api/profile”。

0 个答案:

没有答案