-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazon-app.js
43 lines (38 loc) · 1.28 KB
/
amazon-app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var amazon = require('amazon-product-api'),
app = require('express')();
var amazonClient = amazon.createClient({
awsId: process.env.AMAZON_ASSOC_AWS_ID,
awsSecret: process.env.AMAZON_ASSOC_AWS_SECRET,
awsTag: process.env.AMAZON_ASSOC_AWS_TAG
});
function getBooks(req, res) {
"use strict";
var params = {
keywords: req.params.keywords,
searchIndex: 'Books',
responseGroup: 'Images,ItemAttributes',
sort: 'relevancerank'
};
amazonClient.itemSearch(params).then(function(results) {
var mappedResult = results.map(function (item) {
return {
url: item.DetailPageURL[0],
image: {
url: item.LargeImage[0].URL[0],
height: item.LargeImage[0].Height[0]._,
width: item.LargeImage[0].Width[0]._
},
title: item.ItemAttributes[0].Title[0]
};
});
res.json(mappedResult);
}).catch(function(err) {
console.error(err);
res.status(500).json({error: 'Something went wrong talking to Amazon!'});
});
}
app.get('/books/:keywords', getBooks);
// start app listening
var port = process.env.VCAP_APP_PORT || 3000;
app.listen(port);
console.log('listening at:', port);