This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathapp.js
71 lines (63 loc) · 2.07 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var express = require('express');
var app = express();
// Watson config
var Watson = require('./lib/watson.js');
var watson = new Watson();
var CloudantDB = require('./lib/cloudant.js');
var cloudantDB = new CloudantDB();
var ScrapeData = require('./lib/scrapeData.js');
var scraper = new ScrapeData();
app.use(require('body-parser').json());
//serve static file (index.html, images, css)
app.use(express.static(__dirname + '/public'));
app.get('/reviews/:reviewId', function (req, res, next) {
var reviewId = req.params.reviewId;
return cloudantDB.existingCloudantDoc(reviewId)
.then(function(docExists){
console.log('docExists: ');
console.log(docExists);
scraper.scrapeNumberOfPages(reviewId)
.then(function(options){
// console.log(options)
return scraper.scrapeEveryPage(options);
})
.then(function(options){
console.log(options);
var cloudantDocument = {
_id: options.productId,
productName: options.productName,
starRating: options.starRating,
reviews: options.reviews,
img: options.img
};
return cloudantDB.insertCloudantDoc(cloudantDocument);
})
.then(function(){
return cloudantDB.getCloudantReviews(reviewId);
})
.then(function(reviews){
console.log('right after cloudantDB get cloudant reviews');
// console.log(reviews)
return watson.getSecondOpinion(reviews);
})
.then(function(opinion){
// console.log('before res.send')
res.send(opinion);
})
.catch(next);
});
});
app.get('/cloudant/:reviewId', function (req, res, next) {
console.log('GET request to cloudant/reviewId');
cloudantDB.getCloudantReviews(req.params.reviewId)
.then(function (result) {
console.log(result);
return res.send(result);
})
.catch(next);
});
var port = process.env.PORT || 4000;
app.listen(port, function () {
console.log('Oder API service is in port: ' + port);
cloudantDB.createDB();
});