Book Listing Exercise

node v6.17.0
version: 6.0.0
endpointsharetweet
const express = require("@runkit/runkit/express-endpoint/1.0.0"); const request = require('request'); const Promise = require('bluebird'); const xml2js = require('xml2js'); const app = express(exports); const parseString = Promise.promisify(xml2js.parseString); const requestAsync = Promise.promisify(request); const DEFAULT_PAGE = 1; function mapBook(bookData) { console.log(bookData); const data = bookData.best_book.shift(); const authorName = data.author.pop().name.shift(); const imageUrl = data.image_url.shift(); const title = data.title.shift(); const booksCount = bookData.books_count[0]["_"]; const year = bookData.original_publication_year[0]["_"]; const month = bookData.original_publication_month[0]["_"]; const day = bookData.original_publication_day[0]["_"]; const avgRating = bookData.average_rating; const ratngCount = bookData.ratings_count[0]["_"]; return { authorName, imageUrl, title, booksCount, year, month, day, avgRating, ratngCount }; } function transformResponse(response) { const data = response.GoodreadsResponse.search.shift(); const items = data.results.shift().work; const list = items.map(mapBook); const total = parseInt(data['total-results'].shift(), 10); return { list, total }; } app.all('*', (req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Credentials', true); res.header('Access-Control-Allow-Headers', 'origin, x-requested-with, content-type, credentials, accept, Access-Control-Allow-Origin'); res.header('Access-Control-Allow-Methods', 'GET, OPTIONS'); next(); }); app.get('/search/:term', async function(req, res) { const page = req.query.page || DEFAULT_PAGE; const requestOptions = { baseUrl : 'https://www.goodreads.com/', qs : { key : 'fylVXMzp2hU0m4YFUMSAUg', page, q : req.params.term }, uri : '/search/index.xml' }; try { const searchResponse = await requestAsync(requestOptions); const results = await parseString(searchResponse.body); res.send(transformResponse(results)); } catch(e) { res .status(500) .send(e); } });
Loading…

no comments

    sign in to comment