Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

modernizations #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
env/
node_modules/

157 changes: 84 additions & 73 deletions api.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,109 @@
var MongoClient = require('mongodb').MongoClient;
var http = require('http');
var url = require('url');
var fs = require('fs');

var db_url = "mongodb://localhost:27017/currency";

const MongoClient = require('mongodb').MongoClient;
const http = require('http');
const url = require('url');
const db_url = "mongodb://localhost:27017/currency";
const fs = require("fs");
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
pathname = url.parse(req.url).pathname;
const q = url.parse(req.url, true);
const 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.writeHead(200, {'Content-type': 'text/css'});
const 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){
} 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{
} 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;
} else if (pathname.match(/^\/api\/latest\/$/) || pathname.match(/^\/api\/latest$/)) {
MongoClient.connect(db_url, function (err, dbo) {
const db = dbo.db("currency");
const collection = db.collection('prices');
let fields = {base: 1, date: 1, _id: 0, rates: 1}
let query = {'base': "EUR"}
let conversion = null;
if (q.query['base']) {
query = {'base': q.query['base'].toUpperCase()};
}
}
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"}))
if (q.query['from'] && q.query['to'] && q.query['amount']) {
conversion = {
'from': q.query['from'].toUpperCase(),
'to': q.query['to'].toUpperCase(),
'amount': parseFloat(q.query['amount'].toUpperCase())
};
query['base'] = q.query['from'].toUpperCase();
}


if (q.query['symbols']) {
fields = {base: 1, date: 1, _id: 0}
let symbols = q.query['symbols'].split(',')
for (const each in symbols) {
const symbol = 'rates.' + symbols[each].toUpperCase()
fields[symbol] = 1;
}
}
res.end();
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'});

if (result[0].rates && conversion) {
conversion.result = result[0].rates[conversion.to] * conversion.amount;
res.write(JSON.stringify(conversion));
} else
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()
} else if (pathname.match(/^\/api\/\d{4}([./-])\d{2}\1\d{2}$/)) {
MongoClient.connect(db_url, function (err, dbo) {
const db = dbo.db("currency");
const collection = db.collection('prices');
let fields = {base: 1, date: 1, _id: 0, rates: 1}
let 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){
if (q.query['symbols']) {
fields = {base: 1, date: 1, _id: 0}
let 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();
});
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{
} else {
res.writeHead(404);
res.write('Contents you are looking are Not Found');
res.end();
}
}).listen(8080);
}
}).listen(process.argv.slice(2).length > 0 ? process.argv.slice(2)[0] : 8080);
45 changes: 29 additions & 16 deletions home.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<title>Foreign Exchange Rate API | Free and Best Currency Converter - Ratesapi</title>
<link rel="stylesheet" type="text/css" href="./style.css/">
<title>Foreign Exchange Rate API | Free and Best Currency Converter - Ratesapi</title>
<link rel="stylesheet" type="text/css" href="./style.css/">

<meta name="description" content="Best Foreign exchange rate API to convert one currency from another and returns in JSON format. RateAPI tracks rates daily published by European central bank"/>
<meta name="description"
content="Best Foreign exchange rate API to convert one currency from another and returns in JSON format. RateAPI tracks rates daily published by European central bank"/>
</head>
<body>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-11720509-10"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
window.dataLayer = window.dataLayer || [];

gtag('config', 'UA-11720509-10');
function gtag() {
dataLayer.push(arguments);
}

gtag('js', new Date());

gtag('config', 'UA-11720509-10');
</script>

<h1>Hello Prices API</h1>
<p><a href="/api/latest?from=USD&to=EUR&amount=5">Latest Prices For Euro Currency 2 dollar to euro: %origin%/api/latest?from=USD&to=EUR&amount=5</a></p>
<p><a href="/api/latest">Latest Prices For Euro Currency: %origin%/api/latest</a></p>
<p><a href="/api/latest?symbols=USD,GBP">Latest Prices For Symbols: %origin%/api/latest?symbols=USD,GBP</a>
</p>
<p><a href="/api/latest?base=USD">Latest Prices For US Currency: %origin%/api/latest?base=USD</a></p>

<p><a href="/api/latest">Latest Prices For Euro Currency: http://localhost:8000/api/latest</a></p>
<p><a href="/api/latest?symbols=USD,GBP">Latest Prices For Symbols: http://localhost:8000/api/latest?symbols=USD,GBP</a></p>
<p><a href="/api/latest?base=USD">Latest Prices For US Currency: http://localhost:8000/api/latest?base=USD</a></p>
<p><a href="/api/2018-04-17">Latest Prices For Euro Currency W.r.to. 2018-04-17:
%origin%/api/2018-04-17</a></p>
<p><a href="/api/2018-04-17?symbols=USD,GBP">Latest Prices For Symbols W.r.to. 2018-04-17:
%origin%/api/2018-04-17?symbols=USD,GBP</a></p>
<p><a href="/api/2018-04-17?base=USD">Latest Prices For US Currency W.r.to. 2018-04-17:
%origin%/api/2018-04-17?base=USD</a></p>

<p><a href="/api/2018-04-17">Latest Prices For Euro Currency W.r.to. 2018-04-17: http://localhost:8000/api/2018-04-17</a></p>
<p><a href="/api/2018-04-17?symbols=USD,GBP">Latest Prices For Symbols W.r.to. 2018-04-17: http://localhost:8000/api/2018-04-17?symbols=USD,GBP</a></p>
<p><a href="/api/2018-04-17?base=USD">Latest Prices For US Currency W.r.to. 2018-04-17: http://localhost:8000/api/2018-04-17?base=USD</a></p>

<script type="text/javascript" src="./home.js"></script>
<script type="text/javascript">
for (let e of document.getElementsByTagName("a")) {
e.innerHTML = e.innerHTML.replace(/%origin%/g, location.origin);
}
</script>
</body>
</html>
</html>