-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (64 loc) · 2.03 KB
/
index.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
72
73
74
75
76
77
var opbeat = require('opbeat').start()
var express = require('express');
var multer = require('multer');
var cheerio = require("cheerio");
var request = require("request");
var parse = require('csv-parse');
var upload = multer({ dest: 'uploads/' });
fs = require('fs');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(opbeat.middleware.express())
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
response.render('pages/index');
});
app.post('/upload', upload.single('csv_file'), function (req, res, next) {
console.log(req.file.filename);
res.redirect('/browser?csv='+req.file.filename + '&page=1');
});
app.get('/browser', function(req, res){
var filename = req.query.csv;
var page = parseInt(req.query.page);
fs.readFile('uploads/' + filename, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
parse(data, {relax: true, skip_empty_lines: true}, function(err, lines){
if(page < 1){
page = 1;
} else if (page >= lines.length) {
page = lines.length - 1;
}
// console.log(lines[page]);
var lastPrice = "$" + (parseInt(lines[page][4]) / 100);
var currPrice = "$" + (parseInt(lines[page][5]) / 100);
var priceDrop = "$" + (parseFloat(lines[page][6]));
var productUrl = lines[page][3];
request({
uri: productUrl,
}, function(error, response, body) {
var content = body;
res.render('pages/browser', {
productName: lines[page][2],
merchant: lines[page][0],
productUrl: productUrl,
productId: lines[page][1],
lastPrice: lastPrice,
currentPrice: currPrice,
priceDrop: priceDrop,
prevPage: page - 1,
nextPage: page + 1,
filename: filename,
content: content
});
});
});
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});