Node.js 从Azure函数NodeJS进行API调用时EnotFind

Node.js 从Azure函数NodeJS进行API调用时EnotFind,node.js,azure-functions,Node.js,Azure Functions,我正在尝试使用Azure函数进行API调用。但我的错误率越来越低 { "errno": "ENOTFOUND", "code": "ENOTFOUND", "syscall": "getaddrinfo", "hostname": "https://jsonplaceholder.typicode.com", "host": "https://jsonplaceholder.typicode.com", "port": "80" } 我的代码 var http = re

我正在尝试使用Azure函数进行API调用。但我的错误率越来越低

{
  "errno": "ENOTFOUND",
  "code": "ENOTFOUND",
  "syscall": "getaddrinfo",
  "hostname": "https://jsonplaceholder.typicode.com",
  "host": "https://jsonplaceholder.typicode.com",
  "port": "80"
}
我的代码

var http = require('http');

module.exports = function (context) {
    context.log('JavaScript HTTP trigger function processed a request.');

    var options = {
        host: 'https://jsonplaceholder.typicode.com',
        port: '80',
        path: '/users',
        method: 'GET'
    };

    // Set up the request
    var req = http.request(options, (res) => {
        var body = "";

        res.on("data", (chunk) => {
            body += chunk;
        });

        res.on("end", () => {
            context.res = body;
            context.done();
        });
    }).on("error", (error) => {
        context.log('error');
        context.res = {
            status: 500,
            body: error
        };
        context.done();
    });
    req.end();
};

我如何解决这个问题?

您犯了一些常见错误:

  • 您正在对https URL使用
    http
    模块
  • 将主机值更改为
    jsonplaceholder.typicode.com
  • 对于https协议,端口应为443
更改选项如下:

对于
http

var options = {
        host: 'jsonplaceholder.typicode.com',
        port: '80',
        path: '/users',
        method: 'GET'
    };
对于https:

使用
https
模块发出请求,选项对象应如下所示

 var options = {
            host: 'jsonplaceholder.typicode.com',
            port: '443',
            path: '/users',
            method: 'GET'
    };
演示: