4
0

More than 1 year has passed since last update.

Node.js: fetch の使い方

Last updated at Posted at 2022-03-01

こちらを参考にしました。
node-fetch/node-fetch

ライブラリーのインストール

npm install node-fetch
fetch01.mjs
#! /usr/bin/node

import fetch from 'node-fetch'

const url = 'https://api.github.com/users/ekzemplaro'
const response = await fetch(url)
const body = await response.text()

console.log(body)

実行結果

$ ./fetch01.mjs | jq
{
  "login": "ekzemplaro",
  "id": 7269588,
  "node_id": "MDQ6VXNlcjcyNjk1ODg=",
  "avatar_url": "https://avatars.githubusercontent.com/u/7269588?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/ekzemplaro",
  "html_url": "https://github.com/ekzemplaro",
  "followers_url": "https://api.github.com/users/ekzemplaro/followers",
  "following_url": "https://api.github.com/users/ekzemplaro/following{/other_user}",
  "gists_url": "https://api.github.com/users/ekzemplaro/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/ekzemplaro/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/ekzemplaro/subscriptions",
  "organizations_url": "https://api.github.com/users/ekzemplaro/orgs",
  "repos_url": "https://api.github.com/users/ekzemplaro/repos",
  "events_url": "https://api.github.com/users/ekzemplaro/events{/privacy}",
  "received_events_url": "https://api.github.com/users/ekzemplaro/received_events",
  "type": "User",
  "site_admin": false,
  "name": "Uchida Masatomo",
  "company": null,
  "blog": "https://ekzemplaro.org",
  "location": "Tochigi,Japan",
  "email": null,
  "hireable": null,
  "bio": null,
  "twitter_username": null,
  "public_repos": 33,
  "public_gists": 0,
  "followers": 3,
  "following": 0,
  "created_at": "2014-04-12T03:29:10Z",
  "updated_at": "2022-02-10T08:40:41Z"
}

確認したバージョン

$ node --version
v19.8.1
4
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0