简体   繁体   English

node js api调用不同的域

[英]node js api call with different domain

  • using node js I need to call two different api, which are present in two different domains. 使用节点js我需要调用两个不同的api,它们存在于两个不同的域中。
  • one api works fine which is present in sports domain. 一个api工作正常,体育领域。
  • another api which is present in players domain, which I added now thats where I am getting the error UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 6): TypeError: Cannot read property 'status' of undefined 另一个api存在于玩家域名中,我现在添加了,这就是我收到错误的地方UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 6): TypeError: Cannot read property 'status' of undefined
  • I debugged by putting console but not sure how to fix it. 我通过放置控制台调试,但不知道如何解决它。
  • do I need to change the configuration. 我需要更改配置吗? I even added routers 我甚至添加了路由器
  • providing my code snippet below. 在下面提供我的代码片段。

players domain node js code 玩家域节点js代码

const express = require('express');
const axios = require('axios');
const mime = require('mime-types');

const router = express.Router();
const ResponseUtil = require('../../utils/ResponseUtil');

router.get('/:rank/:jump', (req, res, next) => {
  const { originalUrl } = req;
  //console.log(" originalUrl  ", originalUrl);
  const mode = req.params.rank;
  const value = encodeURIComponent(req.params.jump);
  console.log("document 4--->", mode);
  console.log("for document Testing--->", mode);
           const url = `players/?id=890900-weweew`;

  axios
    .get(url, {
      headers: {
        authorization: req.headers.authorization,
      },
    })
    .then((response) => {
      console.log("document--->", response);
      const file = Buffer.from(response.data.content, 'base64');
      const fileType = mime.contentType(response.data.contentInfo.fileType);
      const fileExtension = response.data.contentInfo.fileType.toLowerCase();
      const fileName = `filename=${response.data.contentInfo.id}.${fileExtension}`;
      res.set('Content-Type', fileType);
      res.set('Content-disposition', `attachment; ${fileName}`);
      res.send(file);
    })
    .catch((e) => {
      res.status(e.response.status).send(e.response.data);
    });

    ResponseUtil.callService(res, url);


});

module.exports = router;

sports domain node js code 体育域节点js代码

const express = require('express');
const router = express.Router();
const ResponseUtil = require('../../utils/ResponseUtil');

router.get('/:rank/:jump', (req, res, next) => {

  const { originalUrl } = req;

  //console.log(" originalUrl  ", originalUrl);

  const mode = req.params.rank;

  const value = encodeURIComponent(req.params.jump);

  const url = `/sports?mode=sdioiosdio`;

  console.log("rank 3--->", mode);
  console.log("for document Testing--->", mode);


  ResponseUtil.callService(res, url);

});

module.exports = router;

According the axios document your requst to url is not accomplished. 根据axios文件,您对url的要求尚未完成。
Maybe the destination is not reachable. 也许目的地无法到达。
So the best error handling as document says is that you check for availability of response in error. 因此,文档中最好的错误处理是您检查错误response可用性。

...
.catch((e) => {
      if(e.response){
        return res.status(e.response.status).send(e.response.data);
      }
      res.status(500).send(e.message || 'Something wrong');
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM