-
Notifications
You must be signed in to change notification settings - Fork 2
/
awesome.js
65 lines (57 loc) · 2.17 KB
/
awesome.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var amazon = require('amazon-product-api');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
var keyword=req.query.keyword;
var client = amazon.createClient({
awsId: "AKIAJRWT3AWHMGGLQUJA",
awsSecret: "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",
awsTag: "hackohio-20"
});
client.itemSearch({
SearchIndex: 'All',
Keywords: keyword,
ResponseGroup: 'ItemAttributes,Offers,Images'
}).then(function(results){
var items = [];
var item;
for(var i = 0; i <10 ; i++){
item= {
detailpageurl: results[i].DetailPageURL,
medimg: results[i].MediumImage[0].URL,
title: results[i].ItemAttributes[0].Title
};
items.push(item);
}
res.json(items);
//var price = results[0].Offers[0].Offer[0].OfferListing[0].Price[0].FormattedPrice;
});
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
app.get('/', function( req,res) {
res.sendfile('ohioHack_frontend.html',{root : __dirname });
});
app.get('/ohioHack_frontend.css', function( req,res) {
res.sendfile('ohioHack_frontend.css',{root : __dirname });
});
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);