11const express = require ( 'express' ) ;
22const app = express ( ) ;
3+ const lookup = require ( 'country-code-lookup' ) ;
4+ const morgan = require ( 'morgan' ) ;
35
46const port = process . env . PORT || 3001 ;
57
6- const { getCountryTable } = require ( './lib/byCountry' ) ;
8+ const { getCountryTable, getJSONData , getJSONDataForCountry } = require ( './lib/byCountry' ) ;
79const { getCompleteTable } = require ( './lib/corona' ) ;
810const { countryUpperCase } = require ( './lib/helpers' ) ;
911
1012
13+ function errorHandler ( error , res ) {
14+ console . error ( error ) ;
15+ return res . send ( 'I am sorry. Something went wrong. Please report it' ) ;
16+ }
17+
18+ app . use ( morgan ( ':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms' ) ) ;
19+
1120app . get ( '/' , ( req , res ) => {
21+ const format = req . query . format ? req . query . format : '' ;
22+
23+ if ( format . toLowerCase ( ) === 'json' ) {
24+ return getJSONData ( ) . then ( result => {
25+ res . setHeader ( 'Cache-Control' , 's-maxage=86400' ) ;
26+ return res . json ( result ) ;
27+ } ) . catch ( error => errorHandler ( error , res ) ) ;
28+ }
29+
1230 return getCompleteTable ( ) . then ( result => {
31+ res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
1332 return res . send ( result ) ;
14- } ) . catch ( error => res . send ( error ) ) ;
33+ } ) . catch ( error => errorHandler ( error , res ) ) ;
1534} ) ;
1635
1736app . get ( '/:country' , ( req , res ) => {
37+
38+ const { country } = countryUpperCase ( req . params ) ;
39+ let lookupObj = null ;
40+ const format = req . query . format ? req . query . format : '' ;
1841
19- let { country } = countryUpperCase ( req . params ) ;
2042 if ( ! country || country === 'All' ) {
43+ if ( format . toLowerCase ( ) === 'json' ) {
44+ return getJSONData ( ) . then ( result => {
45+ res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
46+ return res . json ( result ) ;
47+ } ) . catch ( error => errorHandler ( error , res ) ) ;
48+ }
49+
50+
2151 return getCompleteTable ( ) . then ( result => {
52+ res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
2253 return res . send ( result ) ;
23- } ) . catch ( error => res . send ( error ) ) ;
54+ } ) . catch ( error => errorHandler ( error , res ) ) ;
2455 }
25- return getCountryTable ( country ) . then ( result => {
56+
57+ try {
58+ lookupObj = lookup . byIso ( country )
59+ || lookup . byFips ( country )
60+ || lookup . byCountry ( country ) ;
61+ } catch ( error ) {
62+ lookupObj = lookup . byFips ( country ) || lookup . byCountry ( country ) ;
63+ }
64+ if ( ! lookupObj ) {
65+ return res . send ( `
66+ Country not found.
67+ Try full country name or country code.
68+ Ex:
69+ - /UK: for United Kingdom
70+ - /US: for United States of America.
71+ - /India: for India.
72+ ` ) ;
73+ }
74+ const { iso2 } = lookupObj ;
75+
76+ if ( format . toLowerCase ( ) === 'json' ) {
77+ return getJSONDataForCountry ( iso2 ) . then ( result => {
78+ res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
79+ return res . json ( result ) ;
80+ } ) . catch ( error => errorHandler ( error , res ) ) ;
81+ }
82+
83+ return getCountryTable ( iso2 ) . then ( result => {
84+ res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
2685 return res . send ( result ) ;
27- } ) . catch ( error => res . send ( error ) ) ;
86+ } ) . catch ( error => errorHandler ( error , res ) ) ;
2887} ) ;
2988
89+
3090app . listen ( port , ( ) => console . log ( `Running on ${ port } ` ) ) ;
0 commit comments