This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
api.js
98 lines (94 loc) · 3.65 KB
/
api.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
87
88
89
90
91
92
93
94
95
96
97
98
var MongoClient = require('mongodb').MongoClient;
var http = require('http');
var url = require('url');
var fs = require('fs');
var db_url = "mongodb://localhost:27017/currency";
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
pathname = url.parse(req.url).pathname;
if (pathname.match(/^\/style.css\/$/) || pathname.match(/^\/style.css$/)) {
res.writeHead(200, {'Content-type' : 'text/css'});
var fileContents = fs.readFileSync('./style.css', {encoding: 'utf8'});
res.write(fileContents);
res.end();
}
else if (req.url === '/'){
fs.readFile('./home.html', null, function(err, data){
if(err){
res.writeHead(404);
res.write('Contents you are looking are Not Found');
res.end();
}else{
res.write(data);
res.end();
}
});
}
else if (pathname.match(/^\/api\/latest\/$/) || pathname.match(/^\/api\/latest$/)){
MongoClient.connect(db_url, function(err, dbo) {
var db = dbo.db("currency");
var collection = db.collection('prices');
fields = {base:1, date:1, _id:0, rates: 1}
query = {'base': "EUR"}
if(q.query['base']){
query = {'base': q.query['base'].toUpperCase()}
}
if(q.query['symbols']){
fields = {base:1, date:1, _id:0}
symbols = q.query['symbols'].split(',')
for (each in symbols){
symbol = 'rates.' + symbols[each].toUpperCase()
fields[symbol] = 1;
}
}
var cursor = collection.find(query, {fields: fields}).sort({"date": -1}).limit(1).toArray(function(err, result) {
if (err) throw err;
dbo.close();
if(result[0]){
res.writeHead(200, {'Content-Type': 'text/json'});
res.write(JSON.stringify(result[0]));
}else{
res.writeHead(400, {'Content-Type': 'text/json'});
res.write(JSON.stringify({"error":"Invalid base or symbols"}))
}
res.end();
});
});
}else if (pathname.match(/^\/api\/\d{4}([./-])\d{2}\1\d{2}$/)){
MongoClient.connect(db_url, function(err, dbo) {
var db = dbo.db("currency");
var collection = db.collection('prices');
fields = {base:1, date:1, _id:0, rates: 1}
query = {'base': "EUR", 'date': { $lte: req.url.split('/api/')[1].split('?')[0].toString() } }
if(q.query['base']){
query['base']= q.query['base'].toUpperCase()
}
if(q.query['symbols']){
fields = {base:1, date:1, _id:0}
symbols = q.query['symbols'].split(',')
for (each in symbols){
symbol = 'rates.' + symbols[each].toUpperCase()
fields[symbol] = 1;
}
}
var cursor = collection.find(query, {fields: fields}).sort({date: -1}).toArray(function(err, result) {
if (err){
throw err;
}
dbo.close();
if(result[0]){
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify(result[0]));
}else{
res.writeHead(400, {'Content-Type': 'application/json'});
res.write(JSON.stringify({"error":"Invalid base or symbols"}))
}
res.end();
});
});
}else{
res.writeHead(404);
res.write('Contents you are looking are Not Found');
res.end();
}
}).listen(8080);