簡體   English   中英

如何使用 Node.js https 庫中的 `https.get(...)` 方法調用 http 端點

[英]How to call an http endpoint using the `https.get(...)` method in the Node.js https library

我想調用一個基本端點來解析它公開的 json 數據。 數據是使用我在我的機器上本地運行的 REST 端點提供的。 URL 很簡單,僅在端口 80 上開放(端口 443 上沒有 SSL 或 TLS)。

當我使用以下代碼調用端點時,出現此錯誤:

ncaught NodeError: Protocol "http:" not supported. Expected "https:"

這是我的 typescript 代碼:

var req = https.get(`http://localhost/events/recent`, (response) => {
    var responseCode;
    var responseBody = [];
    response.on("data", (data) => {
        responseBody.push(data);
    });
    response.on("end", (data) => {
        var result = Buffer.concat(responseBody);
        responseCode = response.statusCode;
        if (response.statusCode === 200) {
            processResult(result);
        }
        else {
            console.log(`The Response failed with status code: ${responseCode}`);
        }
    });
});
req.end();

您的問題是您正在使用https庫。 要使用http庫,請將代碼的第一行更改為以下內容:

var req = http.get(`http://localhost/events/recent`, (response) => {

看看您最初是如何擁有https的,這意味着 function 需要一個https起始地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM