【发布时间】:2022-01-28 04:47:56
【问题描述】:
使用 Nodejs 获取 API 的语法是什么,我正在尝试 fetch 但它说 fetch 未定义enter image description here
【问题讨论】:
-
节点没有 fetch,你必须使用像 node-fetch 这样的 polyfill。
-
谢谢我真的很感激这个
使用 Nodejs 获取 API 的语法是什么,我正在尝试 fetch 但它说 fetch 未定义enter image description here
【问题讨论】:
nodejs 中不存在 Fetch。为此,您必须使用一些模块来发出获取请求。
一个例子是:https://www.npmjs.com/package/node-fetch
npm install node-fetch
const fetch = require('node-fetch');
const response = await fetch('https://httpbin.org/post', {method: 'POST', body: 'a=1'});
const data = await response.json();
【讨论】: