Solution: Build an API to Fetch Posts
Take a look at the solution for the coding challenge to build an API to fetch posts and use middleware.
We'll cover the following...
Solution
Here’s a solution for the "Build an API to Fetch Posts" coding challenge:
const axios = require('axios'); // Implement the below function to fetch the posts const fetchPosts = async (limit, offset) => { // Make an API call const posts = await axios.get('https://jsonplaceholder.typicode.com/posts'); // Process the posts based on the limit and offset and the required // data for each post const response = posts.data.slice(offset, offset + limit).map(post => { return { 'id': post.id, 'title': post.title, 'body': post.body } }) // Return the list of posts return response; } // Export the functions module.exports = { fetchPosts }
Solution: Build an API to Fetch Posts
Code explanation:
First, let’s discuss the utils.js
file:
Line 7: We make an API call using
axios.get()
and the URL provided. This ...