使用Express从URL获取JSON对象

使用Express从URL获取JSON对象,json,reactjs,express,Json,Reactjs,Express,在express users.js文件中: router.get('/', function(req, res, next) { fetch('https://www.somwhere.com/users') .then(res => res.json()) .catch(error => console.log(error)); }); module.exports = router; 在我使用的React应用程序的App.js文件中 compone

在express users.js文件中:

router.get('/', function(req, res, next) {
  fetch('https://www.somwhere.com/users')
      .then(res => res.json())
      .catch(error => console.log(error));
});

module.exports = router;
在我使用的React应用程序的App.js文件中

componentDidMount() {
        fetch('/users')
            .then(res => res.json())
            .then(users => this.setState({ users }));
    }
现在它抛出了一个500错误,但它没有捕获错误


您可以在前端(“React”)和后端(“Express”)中使用
axios
下面的代码只是一个示例代码,您可以遵循:


您可以在前端(“React”)和后端(“Express”)中使用
axios
下面的代码只是一个示例代码,您可以遵循:


使用
fetch
在你的
route
中使用外部URL,然后使用
res.json
返回响应谢谢你的帮助,我已经用我现在遇到的东西编辑了上面的代码。在你的
App.js中使用
fetch('/localhost:3000/')
而不是
fetch('/users')
zerosand1s:我不这么认为,我认为他使用了
app.use('/users',userRouter)
,这就是为什么他不使用
router.get('/users')
。在你的
route
中使用
fetch
和外部URL,然后用
res.json
返回响应谢谢你的帮助,我已经用我现在遇到的内容编辑了上面的代码。使用
fetch(“/localhost:3000/”)
在你的
App.js中
不是
fetch(“/users”)
zerosand1s:我不这么认为,我认为他使用
App.use(“/users”,userRouter)
,这就是为什么他不使用
router.get(“/users”)