This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
136 lines (105 loc) · 2.98 KB
/
server.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//To read the .env files
require('dotenv').config() ;
const bodyParser = require('body-parser') ;
const express = require('express') ;
const { getRates , getHistorical ,getSymbols } = require('./lib/fixer-service.js') ;
const { convertCurrency } = require('./lib/free-currency-service.js') ;
const app = express() ;
const port = process.env.PORT || 3000;
app.use(express.static('public')) ;
//To access the node_modules
app.use('/scripts',express.static(`${__dirname}/node_modules`)) ;
//Error Handling
const errorHandler = (err, req, res)=>{
if(err.response) {
res.status(403).send({
title : 'Server responded with an error' ,
message : err.message
}) ;
} else if (err.request ) {
res.status(503).send({
title : 'Unable to communicate with server' ,
message : err.message
}) ;
} else {
res.status(500).send({
title : 'Am unexpected error occurred' ,
message : err.message
}) ;
}
}
//Test
//const love = async ()=>{
// try{
// const data = await getRates() ;
// console.log('outside :' + JSON.stringify(data) ) ;}
// catch(err) {console.log(err) ;}
//}
//
//love() ;
//Fetch latest rates
app.get('/api/rates' , async(req , res)=>{
try{
const data = await getRates() ;
res.setHeader('Content-Type' , 'application/json') ;
res.send(JSON.stringify(data)) ;
} catch(error) {
errorHandler(error, req, res) ;
}
}) ;
app.get('/api/symbols', async(req , res)=>{
try{
const data = await getSymbols() ;
res.setHeader('Content-Type' , 'application/json') ;
res.send(JSON.stringify(data)) ;
}
catch (err){
errorHandler(err , req ,res) ;
}
} );
//Using bodyParser middleware to process req.body
app.use(bodyParser.urlencoded({
extended : true
})) ;
app.use(bodyParser.json()) ;
app.post('/api/convert' , async(req ,res)=>{
try{
const { from ,to } = req.body ;
//console.log(req.body) ;
const data = await convertCurrency( from, to) ;
res.setHeader('Content-Type', 'application/json') ;
res.send(data) ;
//console.log(data) ;
} catch (err){
errorHandler( err, req ,res ) ;
}
} ) ;
app.post('/api/historical' ,async(req , res) => {
try{
const { date } = req.body ;
//console.log(req.body) ;
const data = await getHistorical(date) ;
res.setHeader('Content-Type' , 'application/json') ;
res.send(data) ;
} catch(err) {
errorHandler(err, req , res) ;
}
}) ;
////Test Block
//const test = async()=> {
// const data = await getHistorical('2012-10-11') ;
// console.log(await getRates()) ;
// console.log(data) ;
//
//
//} ;
//
//test() ;
//Routing
app.use((req ,res)=>{
res.sendFile(`${__dirname}/public/index.html`) ;
}) ;
// Listen to port
app.listen(port , ()=>{
console.log(`listening at localhost:${port}`) ;
}) ;