[英]ENOTFOUND issue on https get request
在我的應用程序中,我嘗試使用 https 達到終點,然后得到響應。
路線文件:
router.get("/get-details", (req, res) => {
sampleController.getDetails()
.then((data) => {
console.log("Data is ", data);
res.send(data);
})
.catch(() => {
res.status(500).json({
success: false,
data: null,
message: "Failed to lookup the data",
});
});
});
Controller 文件:
const getDetials = () => {
return new Promise((resolve, reject) => {
const options = {
hostname: "https://jsonplaceholder.typicode.com/",
path: "posts",
method: "GET",
};
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on("data", (d) => {
process.stdout.write(d);
});
});
req.on("error", (error) => {
console.log("Error is ", error);
reject(error);
});
req.end();
});
};
我收到此錯誤:
不知道我在哪里犯了錯誤。 有人知道我在這里做錯了什么嗎?
嘗試在沒有協議的情況下設置 URL 並在選項 object 的路徑前添加/
:
const options = {
hostname: "jsonplaceholder.typicode.com",
path: "/posts",
method: "GET",
};
完整示例:
const getDetials = () => {
return new Promise((resolve, reject) => {
const options = {
hostname: "jsonplaceholder.typicode.com",
path: "/posts",
method: "GET",
};
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on("data", (d) => {
process.stdout.write(d);
});
});
req.on("error", (error) => {
console.log("Error is ", error);
reject(error);
});
req.end();
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.