node.js - 使用 Node.js 和 Express 发出 unirest GET 请求

标签 node.js reactjs express

我无法弄清楚如何发出 unirest GET 请求。我想从我的 API 路由之一内的外部 API 获取数据(使用 Express),然后在我的前端获取该数据(使用 ReactJS)。

谁能给我指出正确的方向?

这就是我已经走了多远:

带有我的 GET 路由的 Express 服务器

const express = require('express');
const unirest = require('unirest');

const app = express();
const port = process.env.PORT || 5000;

app.get('/api/podcasts', (req, res) => {
    //HOW DO I MAKE GET REQUEST EXTERNAL API? WITH UNIREST
});

app.listen(port, () => console.log(`Listening on port ${port}`));

我的 ReactJS 组件:

import React, { Component } from 'react'

export default class Podcasts extends Component {

  state = {
    response: ''
  };

    componentDidMount() {
    this.callApi()
      .then(res => this.setState({ podcasts: res }))
      .catch(err => console.log(err));
  }

  callApi = async () => {
    const response = await fetch('/api/podcasts');
    const body = await response.json();

    if (response.status !== 200) throw Error(body.message);

    return body;
  };

  render() {
    return (
        <div>
          <div>Show data from my state here</div>
        </div>
    );
  }
}

最佳答案

引用here

const express = require('express');
const unirest = require('unirest');

const app = express();
const port = process.env.PORT || 5000;

app.get('/api/podcasts', (req, res) => {
  unirest.get('your url').end((response) => {
    //make sure response should be a JSON object
    res.status(200).send(response)
  });
});

app.listen(port, () => console.log(`Listening on port ${port}`));

编辑 - 将 XML 转换为 JSON

您可以使用 parse - xml2jsonXML 转换为 JSON ,引用下面的例子

let xmlParser = require('xml2json');
let xmlString = `<?xml version="1.0" encoding="UTF-8"?>
<TestScenario>
   <TestSuite name="TS_EdgeHome">
      <TestCaseName name="tc_Login">dt_EdgeCaseHome,dt_EdgeCaseRoute</TestCaseName>
      <TestCaseName name="tc_Logout">dt_EdgeCaseRoute</TestCaseName>
   </TestSuite>
   <TestSuite name="TS_EdgePanel">
      <TestCaseName name="tc_AddContract">dt_EdgeCaseHome,dt_EdgeCaseSpectrum</TestCaseName>
   </TestSuite>
      <TestSuite name="TS_EdgeRoute">
      <TestCaseName name="tc_VerifyContract">dt_EdgeCaseRoute</TestCaseName>
      <TestCaseName name="tc_Payment">dt_EdgeCaseRoute</TestCaseName>
   </TestSuite>
   <TestSuite name="TS_EdgeSpectrum">
      <TestCaseName name="tc_ClientFeedback">dt_EdgeCaseSpectrum</TestCaseName>
   </TestSuite>
</TestScenario>`;

console.log(xmlParser.toJson(xmlString));

关于node.js - 使用 Node.js 和 Express 发出 unirest GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52561002/

相关文章:

javascript - 实例创建(JS)

html - 当点击托管在 web 上的目录的 url 时,如何创建默认模板?

android - :project :react-native-maps Cannot find a version of 'com.android.support:support-compat' that satisfies the version constraints 要求

javascript - 解析时 JSON 中位置 608 处的意外字符串

css - 在 ReactJS 中使用网格模板列,内联样式

node.js 与 express 如何从 url 中删除查询字符串

mongodb - Map Reduce Mongodb Node JS 原生驱动

node.js - 使用哪种 Passport 身份验证

mysql - 如何使用 Express 显示来自 mysql 的图像

javascript - 如何路由嵌入查询字符串的搜索 URL?