NodeJS Fetch (via node-fetch)

JS
S
JavaScript

Simple example of querying NPM

1// Example 1
2// ==================================================
3const fetch = require('node-fetch');
4const chalk = require('chalk');
5
6const uri = `https://registry.npmjs.org/chalk`;
7
8// IIAF
9(() => {
10    console.log(chalk.yellow.bold(`Fetching... ${uri}`));
11    return fetch(uri)
12        .then(res => res.json())
13        .then((data) => {
14            console.log(Object.keys(data).length )
15            if (Object.keys(data).length > 0) {
16                console.log(`${data['name']}`);
17                console.log(`${data['description']}`);
18                console.log(`${data['dist-tags'].latest}`)
19            }
20        })
21        .catch(err => new Error(err));
22})();
23
24
25// Another Example (based on Andre Saltz callbags examples)
26// ==================================================
27const getPerson = async id =>{
28  const res = await fetch(`https://starwars.egghead.training/people/${id}`)
29  return await res.json()
30}
31
32getPerson(1)
33  .then(result => {
34    console.log(result);
35  })
36  .catch(err => {
37    console.error(err);
38  })

Created on 1/29/2018