使用Express从URL获取JSON对象
在express users.js文件中:使用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
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”)
。