-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (72 loc) · 2.13 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
78
79
80
81
82
83
84
85
86
var mysql = require('mysql');
var express = require('express');
var _ = require('underscore');
var cors = require('cors');
var countries = require('./countries.json');
var app = express();
app.use(cors());
app.use(wwwRedirect);
app.use('/',express.static('public'));
function wwwRedirect(req, res, next) {
if (req.headers.host.slice(0, 4) === 'www.') {
var newHost = req.headers.host.slice(4);
return res.redirect(301, req.protocol + '://' + newHost + req.originalUrl);
}
next();
};
var con = mysql.createConnection({
host: "localhost",
user: "admin",
password: "password",
database: 'stackNetwork'
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
//Tags api
app.get('/getData/tags',function(req,res){
con.query("select tagName, GROUP_CONCAT(count order by quarter desc SEPARATOR ', ') as counts from tags_analysis group by tagName",function(error,results){
if(error!=null){
res.send(error);
return;
}
results = results.map(function(res,index){
var counts = res.counts.split(", ");
counts.unshift(index);
return {
tagName:res.tagName,
counts:counts
}
});
res.json({
tags: _.pluck(results,'tagName'),
counts:_.pluck(results,'counts')
});
});
})
//Demographic API
app.get('/getData/:table', function(req, res) {
var table=req.params.table;
con.query('select * from ' +table+' where DATE(creation_date)=(select DATE(MAX(creation_date)) from ' +table+ ' )', function(error, results) {
var creationDate = results[0].creation_date;
if (error) throw error;
var response = {};
var count=0;
_.each(results, function(res) {
count+=res.count;
var country = _.find(countries, function(country) {
return country.name == res.Location;
})
if (country != null && country != undefined) {
response[country.code] = res.count;
}
})
res.json({
creation_Date: creationDate,
count:count,
result: response
});
})
});
app.listen(3000);