Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js React Fetch()我的本地节点Express后端;无法返回get请求字符串值_Node.js_Reactjs_Express_Routing - Fatal编程技术网

Node.js React Fetch()我的本地节点Express后端;无法返回get请求字符串值

Node.js React Fetch()我的本地节点Express后端;无法返回get请求字符串值,node.js,reactjs,express,routing,Node.js,Reactjs,Express,Routing,我正在使用node设置一个本地后端服务器,并尝试使用fetch获取要在我的前端应用程序中呈现的值。我已经得到了大部分的方法,因为一切都在编译,但是我在获取正确的值方面遇到了问题。或者至少,我认为我做的每件事都是正确的 目标是在我的app.js中使用fetch()从server.js中的routefoo获取渲染值 我可以从/foo获得回复,但这不是我发送的内容。我正在发送一个json对象{text:“Foo”} 但是,我在控制台中收到了这个消息:App.js:12响应{type:“basic”,u

我正在使用node设置一个本地后端服务器,并尝试使用
fetch
获取要在我的前端应用程序中呈现的值。我已经得到了大部分的方法,因为一切都在编译,但是我在获取正确的值方面遇到了问题。或者至少,我认为我做的每件事都是正确的

目标是在我的
app.js
中使用
fetch()
server.js中的route
foo
获取渲染值

我可以从
/foo
获得回复,但这不是我发送的内容。我正在发送一个json对象
{text:“Foo”}

但是,我在控制台中收到了这个消息:
App.js:12响应{type:“basic”,url:http://localhost:3000/foo,重定向:false,状态:200,确定:true,…}

知道我做错了什么吗

我是否在路由之间正确获取或发送对象?如何让“Foo”在
行上呈现当前状态为{this.state.message}

中 'App.js`

这是我的app.js:

import React, {Component} from 'react';
import './App.css';

class App extends Component {
    state = {
        message: ""
    };

    componentDidMount() {
        fetch('/foo')
            .then(res => {
                console.log(res);
                return res.json();
                // .then(express => this.setState({ express }))
                // .then(() => console.log(this.state.message));
            });
    }

    render() {
        return (
            <div className="App">
                <h1>Users</h1>
                <p>The current state is {this.state.message}</p>
            </div>
        );
    }
}

export default App;
这是我的foo.js

let express = require('express');
let router = express.Router();

router.get('/', function(req, res) {
    res.send([{text: "Foo"}]);
});

router.post('/', function(req, res) {
    res.send('POST handler for /foo route.');
});

module.exports = router;
res.json()仍在返回承诺,请尝试以下操作:

  componentDidMount() {
    fetch("/foo").then(res =>
      res.json().then(data => {
        console.log("data", JSON.stringify(data, null, 4));
        // here you can set state
        //...
      })
    );
  }

就这样。感谢您的收看:)
  componentDidMount() {
    fetch("/foo").then(res =>
      res.json().then(data => {
        console.log("data", JSON.stringify(data, null, 4));
        // here you can set state
        //...
      })
    );
  }