hacker-news-cli/index.js
Robert Webb a108d90a3d Added sample code for querying the HN API in node. I used node because it takes me much less time but many other languages would work. I wanted to demonstrate the ease of hackernews API.
In order to use, install node.
Run npm install in the repo to generate the node_modules folder.
run node index.js
You should see the HN top stories logged out.
2019-02-01 23:32:17 -08:00

43 lines
978 B
JavaScript

const fetch = require("node-fetch");
// implemented from: https://github.com/HackerNews/API
const HN_PREFIX = "https://hacker-news.firebaseio.com/v0/";
const TOP_STORIES = "topstories";
const ITEM = "item";
function hnFetch(type, id = "") {
const url = id
? `${HN_PREFIX}${type}/${id}.json`
: `${HN_PREFIX}${type}.json`;
return fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json"
}
})
.then(res => {
if (!isStatusOk(res.status)) {
throw res;
}
return res.json();
})
.then(res => res)
.catch(error => console.error(error));
}
function isStatusOk(statusCode) {
return statusCode === 200 || statusCode === 304;
}
async function main() {
const storyIds = await hnFetch(TOP_STORIES);
const stories = await Promise.all(
storyIds.slice(0, 20).map(storyId => hnFetch(ITEM, storyId))
);
console.log(stories.map(story => { delete story.kids; return story; }));
}
main();