【问题标题】:Can't make serverside axios api call behind a firewall无法在防火墙后面进行服务器端 axios api 调用
【发布时间】:2017-05-23 22:51:28
【问题描述】:

我正在研究 axios 以在公司防火墙内用于一些 node.js http 调用 API - 我在第一个障碍中跌倒了。

我在下面找到了一个使用 axios 进行服务器 http 调用的示例

const axios = require('axios');
const API = 'https://jsonplaceholder.typicode.com';

/* GET api listing. */
router.get('/', (req, res) => {
  res.send('api works');
});

// Get all posts
router.get('/posts', (req, res) => {
  // Get posts from the mock api
axios.get(`${API}/posts`, { proxy: { host: 'http://proxy.com', port: 8080}})
//axios.get(`${API}/posts`)
    .then(posts => {
      res.status(200).json(posts.data);
    })
    .catch(error => {
      res.status(500).send(error)

    });
});
module.exports = router;

但是当我在防火墙后面时,我在下面得到一个错误

//http://localhost:3000/api/posts

{
"代码": "ENOTFOUND",
"errno": "ENOTFOUND",
"系统调用": "getaddrinfo",
"主机名": "http://proxy.com",
"主机": "http://proxy.com",
“端口”:8080,
“配置”:{ “转换请求”:{

},
"transformResponse": {

},
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"headers": {
  "Accept": "application/json, text/plain, */*",
  "User-Agent": "axios/0.15.3",
  "host": "jsonplaceholder.typicode.com"
},
"method": "get",
"proxy": {
  "host": "http://proxy.com",
  "port": 8080
},
"url": "https://jsonplaceholder.typicode.com/posts"

当我切换到直接连接到 Internet 并且代理设置是我用于 npm 时,它会起作用 - 我不确定最终的解决方案是在防火墙内部还是外部,但我不知道这样做是针对此 api 的,甚至是针对开发人员的全球性的。任何帮助将不胜感激..

【问题讨论】:

    标签: node.js express mean-stack axios


    【解决方案1】:

    我相信 axios 只想要代理配置中的主机,而不是带有协议的 URL:

    "proxy": {
      "host": "proxy.com",
      "port": 8080
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-06
      • 2015-06-01
      • 2015-08-27
      • 1970-01-01
      • 2011-10-06
      • 2011-01-02
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多