【问题标题】:How to fetch a data from an API in MERN stack web application project?如何从 MERN 堆栈 Web 应用程序项目中的 API 获取数据?
【发布时间】:2019-08-17 19:54:30
【问题描述】:

我正在使用 MERN Stack 构建一个图书馆管理系统,其中 ISBN 号是从免费的 Google Book API 获取的。但我对如何将 API 获取到我的应用程序感到困惑。

【问题讨论】:

  • 你能分享一些你到目前为止尝试过的代码吗?
  • 除非您解决了问题,否则我们无法为您提供帮助'
  • 在 react 中使用 axios lib 处理 api...
  • 我只想知道使用 ReactJS 或 Node.js 从 API 获取数据的方法/想法。如果您也添加代码,那将非常有帮助。谢谢。

标签: node.js reactjs mongodb express mern


【解决方案1】:

您可以使用浏览器实现的 fetch API 或 AJAX 与 ReactJS(或任何前端代码)一起使用

使用nodejs,您可以以类似的方式使用像request, request-promise, node-fetch, axios 这样的包。

使用request 库的示例可用于nodejs

request
  .get('http://google.com/img.png' // api url)
  .on('response', function(response) {
    console.log(response.statusCode) // 200
    console.log(response.headers['content-type']) // 'image/png'
    // save the image somewhere, or render to webpage
  })
  .pipe(request.put('http://yoursite.com/img.png'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    相关资源
    最近更新 更多