Skip to content

Instantly share code, notes, and snippets.

@nkhil
Created November 21, 2019 00:13
Show Gist options
  • Save nkhil/d4285d32a11a0bc7c83c922825c260ea to your computer and use it in GitHub Desktop.
Save nkhil/d4285d32a11a0bc7c83c922825c260ea to your computer and use it in GitHub Desktop.

Work in progress to scrap https://httpstatuses.com/ for snippets about status codes.

const fetch = require('node-fetch');
const cheerio = require('cheerio');

const STATUS_CODES = [200, 201, 400, 500];

const URL = 'https://httpstatuses.com';

async function fetchData(statusCode) {
  try {
    const rawData = await fetch(`${URL}/${statusCode}`);
    const data = await rawData.text();
    getRelevantInfo(data);
  } catch (err) {
    console.log('Error occured in fetchData function');
    throw new Error(err);
  }
}

function getRelevantInfo(data) {
  const $ = cheerio.load(data);
  const regex = /i*×/gm;
  const headline = $('.code')
    .find('h2')
    .first()
    .text()
    .replace(regex, '0');
  console.log('TCL: getRelevantInfo -> headline', headline);
  const summary = $('.code')
    .find('p')
    .first()
    .text();
  console.log('TCL: getRelevantInfo -> summary', summary);
}

async function get() {
  const a = await fetchData(502);
  console.log(a);
}

get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment