scrimba
JS Deep Dive
Hacker News
Hacker News - Formatting Stories
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

To see this lesson you need to be logged in. Joining Scrimba is free and gives you access to 20+ courses.Sign up
Hacker News - Formatting Stories
AboutCommentsNotes
Hacker News - Formatting Stories
Expand for more info
pages
stories.js
run
preview
console
import view from '../utils/view.js';

export default async function Stories(path) {
const stories = await getStories(path);
const hasStories = stories.length > 0;

view.innerHTML = `<div>
${hasStories ? stories.map(story => JSON.stringify(story)) : 'No stories'}
</div>`;
}

async function getStories(path) {
const isHomeRoute = path === '/';
const isNewRoute = path === '/new';
if (isHomeRoute) {
path = '/news';
} else if (isNewRoute) {
path = '/newest';
}
const response = await fetch(`https://node-hnapi.herokuapp.com${path}`);
const stories = await response.json();
return stories;
}

// https://node-hnapi.herokuapp.com

// / (Top) -> /news
// /new (New) -> /newest
// /ask (Ask) -> /ask
// /show (Show) -> /show
Console
/index.html#/
-9:57