简体   繁体   English

我如何在 Azure 函数和 node.js 的 module.exports 之外发出 axios 请求?

[英]How can I make axios requests outside of module.exports with Azure Functions and node.js?

I would like to make the consumption of an api through axios, using azure-functions and node.js我想通过 axios 消耗 api,使用 azure-functions 和 node.js

The issue occurs because I would like to put the request inside a function, but that function will be outside the module.exports.出现此问题是因为我想将请求放在 function 中,但 function 将在 module.exports 之外。

Example:例子:

module.exports = async function (context, request) {
    FN_ObteniendoInformacion({}, context)
}

const FN_ObteniendoInformacion= async (dataJson, context) => {
   //Axios Logic
})

I thank you in advance我提前谢谢你

Just define your Axios Logic as a method and call the method in the function, suppose your request still is within the function. Maybe you could refer to my code.把你的Axios逻辑定义成一个方法,然后调用function里面的方法,假设你的请求还在function里面。也许你可以参考我的代码。

var axios = require('axios');  
var url='https://google.com'
const dorequest=function axiosTest( ) {
    return axios.get(url).then(response => {
      // returning the data here allows the caller to get it through another .then(...)

      return response;

    }).catch(err =>{
        console.log(err);
      })
  }

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    dorequest().then(function(response){
        console.log(response);

    });
    context.res = {
        // status: 200, /* Defaults to 200 */
        body: "ok"
    };

}

在此处输入图像描述

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

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