gpt4 book ai didi

Node.js http/node-fetch ETIMEDOUT no longer works(Node.js http/node-fetch ETIMEDOUT不再工作)

转载 作者:bug小助手 更新时间:2023-10-25 22:59:50 25 4
gpt4 key购买 nike



I'm writing an API in Node.JS to protect my API keys for a basic weather app SPA.

我正在Node.js中编写一个API来保护我的基本天气应用程序SPA的API密钥。


Initially my http requests got through fine. The next day (today), with no changes to the request, it fails and I am subjected to a 90-second delay before I receive an unhelpful ETIMEDOUT response.

起初,我的http请求通过得很好。第二天(今天),在没有更改请求的情况下,它失败了,在收到无用的ETIMEDOUT响应之前,我受到了90秒的延迟。


connect ETIMEDOUT 2600:1407:3c00:d90::116:80

连接ETIMEDOUT 2600:1407:3C00:D90::116:80


I tried switching to node-fetch and got similar results.
I also tried a different api but got the same result, so it must be local to my program.

我尝试切换到Node-Fetch,得到了类似的结果。我还尝试了不同的API,但得到了相同的结果,因此它必须是我的程序的本地API。


Can anybody see what's wrong here?

有谁看得出这里出了什么问题吗?


const express = require('express');
const app = express();
const PORT = 8080;
const http = require('http');
const fetch = require('node-fetch'); /* try using node-fetch */



app.use(express.json());

app.listen(
PORT,
() => console.log(`it's alive on http://localhost:${PORT}`)
);

/* wetter, german for weather */
app.get('/wetter', (req, res) => {

console.log('got a wetter request');
var city = req.query.city;
if(!city){
res.status(400).send({message: 'Specify a city!'})
}
console.log(`city: ${city}`);

//let url = `http://api.weatherapi.com/v1/current.json?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&q=${city}&aqi=no`;

/* weatherapi.com failing. Try weather.gov. */
let url = 'http://api.weather.gov/';

console.log(url);
http.get(url, (res) => {
let body = '';

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

res.on('end', () => {
try {
let json = JSON.parse(body);
// Do stuff.
console.log(json);
} catch(error) {
console.error('TRY error: ' + error.message);
};
});

}).on('error', (error) => {
console.error('GET error: ' + error.message);
})

/* http failing. Try node-fetch */
//let settings = { method: "GET", timeout: "1000" };
//fetch(url, settings)
// .then(res => res.json())
// .then((json) => {
// console.log(json);
// });

console.log('Done. Sending response.');

res.status(200).send({
city: `${city}`,
forecast: 'forecast-data'
})
});


更多回答

Note that Node 18 and up have support for the standard fetch API. Best to move on to that.

请注意,Node 18及以上版本支持标准的fetch API。最好继续下去。

优秀答案推荐

Both http and fetch apis are working fine for me. Make sure you are using the right node version.

Http和Fetch API对我来说都工作得很好。确保您使用的是正确的节点版本。


FYI I am using Node Version 18.17.1

仅供参考,我使用的是节点版本18.17.1


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com