@@ -3,25 +3,49 @@ const app = express();
33
44const port = process . env . PORT || 3001 ;
55
6- const { getCountryTable } = require ( './lib/byCountry' ) ;
6+ const { getCountryTable, getJSONData , getJSONDataForCountry } = require ( './lib/byCountry' ) ;
77const { getCompleteTable } = require ( './lib/corona' ) ;
88
99app . get ( '/' , ( req , res ) => {
10+ const format = req . query . format ? req . query . format : '' ;
11+
12+ if ( format . toLowerCase ( ) === 'json' ) {
13+ return getJSONData ( ) . then ( result => {
14+ return res . json ( result ) ;
15+ } ) . catch ( error => res . send ( error ) ) ;
16+ }
17+
1018 return getCompleteTable ( ) . then ( result => {
1119 return res . send ( result ) ;
1220 } ) . catch ( error => res . send ( error ) ) ;
1321} ) ;
1422
1523app . get ( '/:country' , ( req , res ) => {
16- let { country } = req . params ;
24+ const { country } = req . params ;
25+ const format = req . query . format ? req . query . format : '' ;
26+
1727 if ( ! country || country === 'all' ) {
28+ if ( format . toLowerCase ( ) === 'json' ) {
29+ return getJSONData ( ) . then ( result => {
30+ return res . json ( result ) ;
31+ } ) . catch ( error => res . send ( error ) ) ;
32+ }
33+
1834 return getCompleteTable ( ) . then ( result => {
1935 return res . send ( result ) ;
2036 } ) . catch ( error => res . send ( error ) ) ;
2137 }
38+
39+ if ( format . toLowerCase ( ) === 'json' ) {
40+ return getJSONDataForCountry ( country ) . then ( result => {
41+ return res . json ( result ) ;
42+ } ) . catch ( error => res . send ( error ) ) ;
43+ }
44+
2245 return getCountryTable ( country ) . then ( result => {
2346 return res . send ( result ) ;
2447 } ) . catch ( error => res . send ( error ) ) ;
2548} ) ;
2649
50+
2751app . listen ( port , ( ) => console . log ( `Running on ${ port } ` ) ) ;
0 commit comments