- fetch を node.js でも使えるようにするやつ
- node v18 からは built-in で fetch が node に入るのでそれまでのつなぎ
- 基本的な操作感はブラウザの Fetch API と同じぽい
- 違いは https://github.com/node-fetch/node-fetch/blob/main/docs/v3-LIMITS.md あたり
v3 から pure esm package になっていて、古い node.js 環境だと使えない可能性が高い。
# v3 (esm only)
$ npm i node-fetch@3
$ npm i -D @types/node-fetch@3
# v2
$ npm i node-fetch@2
$ npm i -D @types/node-fetch@2
import fetch from 'node-fetch'
// get
const getResponse = await fetch('https://api.github.com/users/github')
const getResponseData = await getResponse.json()
console.log(getResponseData)
// post
const postResponse = await fetch('https://httpbin.org/post', {
method: 'post',
body: JSON.stringify({ hello: 'world' }),
headers: {'Content-Type': 'application/json'},
})
const postResponseData = await postResponse.json()
console.log(postResponseData)