【问题标题】:Cannot fetch resource from one localhost server to another无法从一个本地主机服务器获取资源到另一个
【发布时间】:2019-05-17 02:03:26
【问题描述】:

我有两台服务器,其中一台是用于 React 的 Webpack 服务器 (localhost:3000),另一台是用于 Express 服务器 (localhost:4000)。我读到我需要在我的package.json 文件中设置代理。好的,我做到了。但它仍然不起作用。我试图在fetch 函数上设置标题。还是不行。并且控制台中没有任何错误。

这是我的代码。

package.json

{
  "name": "newsfeed",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:4000",
  "dependencies": {
    "npm-run-all": "^4.1.5",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "react-scripts": "2.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "dev": "run-p server start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "server": "node server_demo/server.js"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './style.css';

class NewsBlock extends React.Component {
    constructor() {
        super();
        this.state = {
            data: ''
        }
    }
    componentDidMount() {
        fetch('localhost:4000/',{
        credentials: 'include',
        headers: {
            'Content-Type': 'application/json'
        }
    }).then(res => res.json()).then(data => this.setState({data}));
    }
    render() {
        return <div>
            {this.state.data}
              </div>
    }
}

ReactDOM.render(
    <NewsBlock/>,
    document.getElementById('root')
)

server_demo/server.js

const exp = require('express');
const app = exp();

app.listen(4000, () => console.log('Listening on 4000'));

app.use(exp.static('./'));


app.get('/', (req, res) => {
    res.setHeader('Content-Type', 'application/json');
    res.json({
        "glossary": {
            "title": "example glossary"
        }
    });

})

【问题讨论】:

    标签: reactjs express proxy localhost package.json


    【解决方案1】:

    您不需要package.json 中的代理字段。将CORS middleware 添加到您的 Express 服务器。

    app.use(cors({
      origin: true,
      credentials: true,
    }))
    

    【讨论】:

      【解决方案2】:

      您需要告诉您的服务器可以接收来自其他地方的请求。您可以为此使用跨源请求。

      在您的 Express 服务器上安装 CORS

      使用npm i cors 在您的服务器上安装cors。

      导入服务器

      在您的导入中添加const cors = require("cors");

      允许其他服务器连接

      然后添加这个app.use(cors());

      把它放在一起,你的服务器应该看起来像这样:

      const exp = require('express');
      const cors = require('cors'); 
      
      const app = exp();
      app.use(cors());
      
      app.listen(4000, () => console.log('Listening on 4000'));
      
      app.use(exp.static('./'));
      
      
      app.get('/', (req, res) => {
          res.setHeader('Content-Type', 'application/json');
          res.json({
              "glossary": {
                  "title": "example glossary"
              }
          });
      
      })
      

      应该这样做。希望这可以帮助。

      【讨论】:

        猜你喜欢
        • 2017-12-22
        • 2014-04-27
        • 1970-01-01
        • 2012-07-02
        • 1970-01-01
        • 1970-01-01
        • 2018-10-18
        • 2019-02-16
        • 1970-01-01
        相关资源
        最近更新 更多