如何将信息从Node传递到React

时间:2019-07-19 09:07:19

标签: javascript node.js reactjs

我有一个需要解析为React的对象。 我正在尝试将“行”对象(在节点函数中)移到React状态。 下面的2段代码在不同的页面上!

另一个问题是  GET http://localhost:3000/new net :: ERR_CONNECTION_REFUSED

我目前都在本地运行这两个程序 反应http://localhost:3001/ 节点-http://localhost:3000/

对此有类似的问题,但两个问题我都找不到答案! 谢谢

router.get("/new", (req, res) => {
let parentList = sql.fetchAllParents(function(err, rows) {
  res.setHeader("Access-Control-Allow-Origin", "http://localhost:3001");
    if (err) throw err;
    res.render('new', {parents: rows});
  });
});

componentDidMount() {
   fetch(`http://localhost:3000/new`).then(response => {
      console.log(response)
      return response.json();
   }).then(data => {
      // Work with JSON data here
      console.log(data,'data');
   }).catch(err => {
      // Do something for an error here
      console.log("Error Reading data " + err);
   });
}

1 个答案:

答案 0 :(得分:0)

从节点“新” api发送响应。像这样:

router.get("/new", (req, res) => {
......
......
res.send({status:200,parents:data})

});