TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Fetch API has landed into Node.js

679 点作者 yamafaktory超过 3 年前

27 条评论

inglor超过 3 年前
Hey, Node core person here (and the person who triggered the land) - we&#x27;re super excited for this and would love help and feedback.<p>This is still experimental and we&#x27;d love to hear from the community what you&#x27;d like to see.
评论 #30162547 未加载
评论 #30166605 未加载
评论 #30164925 未加载
评论 #30164098 未加载
评论 #30162058 未加载
评论 #30162218 未加载
评论 #30163133 未加载
评论 #30162006 未加载
评论 #30166667 未加载
评论 #30163606 未加载
评论 #30164195 未加载
评论 #30162383 未加载
评论 #30168481 未加载
评论 #30164919 未加载
评论 #30163445 未加载
评论 #30163388 未加载
评论 #30162527 未加载
评论 #30164180 未加载
评论 #30170663 未加载
pseudosavant超过 3 年前
Every Node.js dev should be thanking the Deno creators. It has forced them off of their &quot;this is how Node does it&quot; complacency. Most improvements I notice and care about on Node these days are all things that Deno already supports (fetch, ES modules, async std lib uses promises, etc).<p>The Web Platform APIs aren&#x27;t perfect, but they are better, and more thoroughly thought out than Node&#x27;s.
评论 #30177378 未加载
thyrox超过 3 年前
My biggest issue with node when I was working on it briefly was I couldn&#x27;t do the &#x27;import&#x27; statements like wepback. Are they supported too now?<p>I&#x27;m not a web developer so I had a very hard time understanding why there are so many different type of imports in JavaScript like require, import, umd, amd, etc and which one works in browser and which one works in node?<p>Also why do so many libraries have this strange 5 line header code for this umd, amd, business. Is that to make their packages work with nodejs?<p>Does anyone who knows enough JavaScript point me in the right direction about it. I find all this very confusing.
评论 #30163794 未加载
评论 #30162108 未加载
评论 #30162144 未加载
评论 #30163427 未加载
评论 #30162106 未加载
评论 #30169555 未加载
评论 #30162249 未加载
评论 #30162131 未加载
评论 #30164027 未加载
ezekg超过 3 年前
One thing I hate about fetch() is that you can&#x27;t manually follow redirects. Using { redirect: &#x27;manual&#x27; } doesn&#x27;t expose the Location header of the redirect, so it&#x27;s essentially useless. I know that node-fetch fixed this issue, so I hope the official Node fetch() does not have the same problem.
评论 #30162700 未加载
nfriedly超过 3 年前
I love fetch, except for the way that it combines headers. If more than one of the same header is set (such as multiple set-cookie headers), it combines them all into a single comma-separated string. I know this is allowed in the HTTP spec, and it&#x27;s probably even a sensible default. But the fetch spec doesn&#x27;t allow any access to the raw headers, so there&#x27;s no straightforward way to get the original uncombined headers back. Set-Cookie headers, in particular, often contain commas in their expiration dates, so just splitting around `, ` will lead to problems.<p>I took a look through the source of this new fetch API, and it seems to have inherited that wart: <a href="https:&#x2F;&#x2F;github.com&#x2F;nodejs&#x2F;undici&#x2F;blob&#x2F;2dd3437e20c5a3cc466226c544f048351baffc78&#x2F;lib&#x2F;fetch&#x2F;headers.js#L103" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nodejs&#x2F;undici&#x2F;blob&#x2F;2dd3437e20c5a3cc466226...</a><p>I&#x27;ve argued with the authors of the fetch spec about this before, and ultimately settled on using <a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;set-cookie-parser#user-content-splitcookiesstringcombinedsetcookieheader" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;set-cookie-parser#user-content...</a> to work around this flaw. (For clarity, I published the package, but chrusart wrote that method - <a href="https:&#x2F;&#x2F;github.com&#x2F;nfriedly&#x2F;set-cookie-parser&#x2F;pull&#x2F;19" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nfriedly&#x2F;set-cookie-parser&#x2F;pull&#x2F;19</a>)
评论 #30166067 未加载
jitl超过 3 年前
There’s an NPM package containing this implementation called ‘undici’ that you can install in previous versions of Node; the package.json claims support back to 12.x.<p><a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;undici" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;undici</a><p>However the fetch implementation itself is documented as unstable and only supports Node 16.x. I believe this is because it is not quite spec compliant yet, so there is latitude for breaking changes that make it more spec compliant.
krossitalk超过 3 年前
As someone browser focused this surprised me. I&#x27;ve been using fetch() for years now.
评论 #30163396 未加载
评论 #30163507 未加载
评论 #30165115 未加载
easton超过 3 年前
For those of us unfamiliar, how long until stuff comes out of experimental?
评论 #30161964 未加载
评论 #30161978 未加载
esamatti超过 3 年前
Does this contain `request.formData()` and `FormData`?<p><a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;Request&#x2F;formData" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;Request&#x2F;for...</a> <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;FormData" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;FormData</a>
评论 #30163364 未加载
stevage超过 3 年前
Out of curiosity, what took so long? It&#x27;s a small API that has been stable for a long time? I&#x27;m probably missing something.
评论 #30171786 未加载
chx超过 3 年前
Good for NodeJS<p>However, I feel the fetch API is completely botched because it lacks timeout support. I have cobbled some together for <a href="https:&#x2F;&#x2F;github.com&#x2F;Backblaze&#x2F;gists&#x2F;pull&#x2F;8" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Backblaze&#x2F;gists&#x2F;pull&#x2F;8</a> but gosh. I really hope all that is actually not necessary :&#x2F;
评论 #30166052 未加载
Meic超过 3 年前
Undici seems to be an HTTP 1.1 client. Is there any work or timescale to provide HTTP 2 support?
评论 #30164873 未加载
评论 #30162806 未加载
songzme超过 3 年前
sorry if this is a stupid question, but how do I try this out? I ran node with the experimental flag on a file that calls fetch:<p>node server.js --experimental-fetch<p>Got &#x27;fetch is not defined&#x27; reference error.
评论 #30162414 未加载
sealthedeal超过 3 年前
Ive used Fetch in the past, great lib. I&#x27;m curious why Node core has decided to make this an official part of Node? From one perspective there could be a hard argument that this should never be a part of Node as it is an external library and not a building block of the language? Almost feels like feature creep for the core language? I know that&#x27;s not the intent, but wanting to understand the logic behind this decision :)
评论 #30163080 未加载
评论 #30163197 未加载
评论 #30163091 未加载
评论 #30163089 未加载
评论 #30163069 未加载
mcraiha超过 3 年前
Why it took so long? e.g. Deno had fetch support for ages
评论 #30162211 未加载
评论 #30162221 未加载
评论 #30163769 未加载
bachmeier超过 3 年前
Good that some people are happy about this, but it really is pointless to post a link to &quot;lib: add fetch&quot; followed by code diffs. HN should have a requirement that there be an actual post, rather than a discussion starter for those in a particular community. All you&#x27;d have to do is type in a brief text post with an explanation and then link to the code diffs for those interested.
评论 #30163294 未加载
outside1234超过 3 年前
Thank god - no more node-fetch shims for node.js!
quickthrower2超过 3 年前
Funny how my brain works, I thought it was there already, but I must have got so used to having it as a dependency I forgot that it was a dependency not built in. To be fair I don&#x27;t &quot;Node&quot; all the time! It&#x27;s on and off.
adam_arthur超过 3 年前
Awesome work! This was one of the main remaining pain points I had with node.
siquick超过 3 年前
Any reason to use fetch if I’ve been happy with axios for years?
bricss超过 3 年前
Try adv fetch-alike with HTTP2 support<p><a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;rekwest" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;rekwest</a>
tekstar超过 3 年前
Can you set more than one cookie in the request and read more than one cookie in the response? Was shocked when I learnt that&#x27;s not supported by the spec..
评论 #30166050 未加载
pjc50超过 3 年前
Brief explanation?
评论 #30161976 未加载
评论 #30164387 未加载
mariogintili超过 3 年前
I love that the most popular language&#x27;s core lib now brings a trivial bit of code available in every other platform and its a celebrated super achievement<p>software, why do you suck now?
评论 #30163550 未加载
评论 #30163605 未加载
itsbits超过 3 年前
Which NodeJS versions will this be landed to?
评论 #30162818 未加载
stereocodes超过 3 年前
Wow I can&#x27;t believe how light years ahead Deno is than Node. You want imports you want fetch you want TS, use Deno.
评论 #30168254 未加载
评论 #30168636 未加载
评论 #30169087 未加载
saos超过 3 年前
Noicee