From 0e506e89e4bf2cf0a7fe6560171466e2c4c5c45c Mon Sep 17 00:00:00 2001 From: yosef langer Date: Tue, 5 Nov 2019 13:39:43 +0200 Subject: [PATCH 1/2] Update api.js --- api.js | 55 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/api.js b/api.js index bfae436..afc4bc9 100644 --- a/api.js +++ b/api.js @@ -1,16 +1,16 @@ -var MongoClient = require('mongodb').MongoClient; -var http = require('http'); -var url = require('url'); -var fs = require('fs'); +let MongoClient = require('mongodb').MongoClient; +let http = require('http'); +let url = require('url'); +let fs = require('fs'); -var db_url = "mongodb://localhost:27017/currency"; +let db_url = "mongodb://localhost:27017/currency"; http.createServer(function (req, res) { - var q = url.parse(req.url, true); + let 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'}); + let fileContents = fs.readFileSync('./style.css', {encoding: 'utf8'}); res.write(fileContents); res.end(); } @@ -28,27 +28,38 @@ http.createServer(function (req, res) { } 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"} + const db = dbo.db("currency"); + const collection = db.collection('prices'); + let fields = {base:1, date:1, _id:0, rates: 1} + let query = {'base': "EUR"} if(q.query['base']){ - query = {'base': q.query['base'].toUpperCase()} + query = {'base': q.query['base'].toUpperCase()}; } + 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} - symbols = q.query['symbols'].split(',') + 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}).limit(1).toArray(function(err, result) { + let 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'}); + + if(result[0].rates&&conversion){ + result[0].conversion={...conversion,result:result[0].rates[conversion.to]}; + } + res.write(JSON.stringify(result[0])); }else{ res.writeHead(400, {'Content-Type': 'text/json'}); @@ -60,22 +71,22 @@ http.createServer(function (req, res) { }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() } } + 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(',') + 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) { + let cursor = collection.find(query, {fields: fields}).sort({date: -1}).toArray(function(err, result) { if (err){ throw err; } @@ -85,7 +96,7 @@ http.createServer(function (req, res) { res.write(JSON.stringify(result[0])); }else{ res.writeHead(400, {'Content-Type': 'application/json'}); - res.write(JSON.stringify({"error":"Invalid base or symbols"})) + res.write(JSON.stringify({"error":"Invalid base or symbols"})); } res.end(); }); @@ -95,4 +106,4 @@ http.createServer(function (req, res) { 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); From 402045d91ed351bacd96a77f6534e182dfde6d4f Mon Sep 17 00:00:00 2001 From: yosef langer Date: Tue, 5 Nov 2019 14:26:22 +0200 Subject: [PATCH 2/2] modified code into a bit more modern js es6 , replacing var with let and const, also some variables were global unnecresarily. made port 8080 optional, just specify on the command line number eg node api.js 8080 . added additional api option to convert currency directly like so: api/latest?from=usd&to=eur&amount=5 responds the conversion immediatly --- .gitignore | 1 + api.js | 166 ++++++++++++++++++++++++++--------------------------- home.html | 45 +++++++++------ 3 files changed, 113 insertions(+), 99 deletions(-) diff --git a/.gitignore b/.gitignore index 2ee90b2..eaccb5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea env/ node_modules/ diff --git a/api.js b/api.js index afc4bc9..6b56ba8 100644 --- a/api.js +++ b/api.js @@ -1,109 +1,109 @@ -let MongoClient = require('mongodb').MongoClient; -let http = require('http'); -let url = require('url'); -let fs = require('fs'); - -let 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) { - let 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'}); - let 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) { - const db = dbo.db("currency"); - const collection = db.collection('prices'); - let fields = {base:1, date:1, _id:0, rates: 1} - let query = {'base': "EUR"} - - if(q.query['base']){ - query = {'base': q.query['base'].toUpperCase()}; - } - 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 (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()}; + } + 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(); } - } - let 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'}); - - if(result[0].rates&&conversion){ - result[0].conversion={...conversion,result:result[0].rates[conversion.to]}; - } - - 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['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) { - 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() + } 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} + if (q.query['symbols']) { + fields = {base: 1, date: 1, _id: 0} let symbols = q.query['symbols'].split(',') - for (each in symbols){ + for (each in symbols) { symbol = 'rates.' + symbols[each].toUpperCase() fields[symbol] = 1; } } - let 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(process.argv.slice(2).length>0?process.argv.slice(2)[0]:8080); + } +}).listen(process.argv.slice(2).length > 0 ? process.argv.slice(2)[0] : 8080); diff --git a/home.html b/home.html index 6f91887..66ad3c3 100644 --- a/home.html +++ b/home.html @@ -1,32 +1,45 @@ - Foreign Exchange Rate API | Free and Best Currency Converter - Ratesapi - + Foreign Exchange Rate API | Free and Best Currency Converter - Ratesapi + - +

Hello Prices API

+

Latest Prices For Euro Currency 2 dollar to euro: %origin%/api/latest?from=USD&to=EUR&amount=5

+

Latest Prices For Euro Currency: %origin%/api/latest

+

Latest Prices For Symbols: %origin%/api/latest?symbols=USD,GBP +

+

Latest Prices For US Currency: %origin%/api/latest?base=USD

-

Latest Prices For Euro Currency: http://localhost:8000/api/latest

-

Latest Prices For Symbols: http://localhost:8000/api/latest?symbols=USD,GBP

-

Latest Prices For US Currency: http://localhost:8000/api/latest?base=USD

+

Latest Prices For Euro Currency W.r.to. 2018-04-17: + %origin%/api/2018-04-17

+

Latest Prices For Symbols W.r.to. 2018-04-17: + %origin%/api/2018-04-17?symbols=USD,GBP

+

Latest Prices For US Currency W.r.to. 2018-04-17: + %origin%/api/2018-04-17?base=USD

-

Latest Prices For Euro Currency W.r.to. 2018-04-17: http://localhost:8000/api/2018-04-17

-

Latest Prices For Symbols W.r.to. 2018-04-17: http://localhost:8000/api/2018-04-17?symbols=USD,GBP

-

Latest Prices For US Currency W.r.to. 2018-04-17: http://localhost:8000/api/2018-04-17?base=USD

- - + - \ No newline at end of file +