99 getJSONDataForCountry,
1010} = require ( './lib/byCountry' ) ;
1111const { getCompleteTable, getGraph } = require ( './lib/corona' ) ;
12- const { lookupCountry, htmlTemplate } = require ( './lib/helpers' ) ;
12+ const { lookupCountry, htmlTemplate, footer } = require ( './lib/helpers' ) ;
1313const { getLiveUpdates } = require ( './lib/reddit.js' ) ;
1414const { getWorldoMetersTable } = require ( './lib/worldoMeters.js' ) ;
1515const { helpContent, countryNotFound } = require ( './lib/constants' ) ;
@@ -18,12 +18,17 @@ const app = express();
1818const port = process . env . PORT || 3001 ;
1919const IS_CURL_RE = / \b c u r l \b / im;
2020
21- function errorHandler ( error , res ) {
21+ function errorHandler ( error , req , res ) {
2222 console . error ( error ) ;
23- return res . status ( 500 ) . send ( htmlTemplate ( `
23+ const body = `
2424 I am sorry. Something went wrong. Please report it\n
2525 ${ error . message }
26- ` ) ) ;
26+ ${ footer ( new Date ) }
27+ ` ;
28+ if ( req . isCurl ) {
29+ return body ;
30+ }
31+ return res . status ( 500 ) . send ( htmlTemplate ( body ) ) ;
2732}
2833
2934app . set ( 'json escape' , true ) ;
@@ -47,58 +52,60 @@ app.use(helmet.referrerPolicy({ policy: 'strict-origin-when-cross-origin' }));
4752app . use ( morgan ( ':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms' ) ) ;
4853app . use ( ( req , res , next ) => {
4954 res . setHeader ( 'Cache-Control' , 'no-cache' ) ;
55+ req . isCurl = IS_CURL_RE . test ( req . headers [ 'user-agent' ] ) ;
5056 next ( ) ;
5157} ) ;
5258
5359app . get ( '/' , ( req , res ) => {
54- const isCurl = IS_CURL_RE . test ( req . headers [ 'user-agent' ] ) ;
60+ const isCurl = req . isCurl ;
5561 const format = req . query . format ? req . query . format : '' ;
5662 const minimal = req . query . minimal === 'true' ;
5763 const emojis = req . query . emojis === 'true' ;
5864 const top = req . query . top ? Number ( req . query . top ) : 1000 ;
59- const source = req . query . source ? Number ( req . query . source ) : 1 ;
65+ const source = req . query . source ? Number ( req . query . source ) : 2 ;
66+
67+ if ( source === 1 ) {
68+ if ( format . toLowerCase ( ) === 'json' ) {
69+ return getJSONData ( ) . then ( result => {
70+ return res . json ( result ) ;
71+ } ) . catch ( error => errorHandler ( error , req , res ) ) ;
72+ }
6073
61- if ( source === 2 ) {
62- return getWorldoMetersTable ( { isCurl, emojis, minimal, top, format} )
74+ return getCompleteTable ( { isCurl, emojis, minimal, top } )
6375 . then ( result => {
6476 return res . send ( result ) ;
65- } ) . catch ( error => errorHandler ( error , res ) ) ;
77+ } ) . catch ( error => errorHandler ( error , req , res ) ) ;
6678 }
6779
68- if ( format . toLowerCase ( ) === 'json' ) {
69- return getJSONData ( ) . then ( result => {
70- return res . json ( result ) ;
71- } ) . catch ( error => errorHandler ( error , res ) ) ;
72- }
73-
74- return getCompleteTable ( { isCurl, emojis, minimal, top } )
80+ return getWorldoMetersTable ( { isCurl, emojis, minimal, top, format} )
7581 . then ( result => {
7682 return res . send ( result ) ;
77- } ) . catch ( error => errorHandler ( error , res ) ) ;
83+ } ) . catch ( error => errorHandler ( error , req , res ) ) ;
84+
7885} ) ;
7986
8087app . get ( '/updates' , ( req , res ) => {
81- const isCurl = IS_CURL_RE . test ( req . headers [ 'user-agent' ] ) ;
88+ const isCurl = req . isCurl ;
8289 const format = req . query . format ? req . query . format : '' ;
8390
8491 if ( format . toLowerCase ( ) === 'json' ) {
8592 return getLiveUpdates ( { json : true , isCurl } ) . then ( result => {
8693 return res . json ( result ) ;
87- } ) . catch ( error => errorHandler ( error , res ) ) ;
94+ } ) . catch ( error => errorHandler ( error , req , res ) ) ;
8895 }
8996
9097 return getLiveUpdates ( { json : false , isCurl } ) . then ( result => {
9198 return res . send ( result ) ;
92- } ) . catch ( error => errorHandler ( error , res ) ) ;
99+ } ) . catch ( error => errorHandler ( error , req , res ) ) ;
93100} ) ;
94101
95102app . get ( [ '/:country/graph' , '/graph' ] , ( req , res ) => {
96103 const { country } = req . params ;
97- const isCurl = IS_CURL_RE . test ( req . headers [ 'user-agent' ] ) ;
104+ const isCurl = req . isCurl ;
98105 if ( ! country ) {
99106 return getGraph ( { isCurl } )
100107 . then ( result => res . send ( result ) )
101- . catch ( error => errorHandler ( error , res ) ) ;
108+ . catch ( error => errorHandler ( error , req , res ) ) ;
102109 }
103110 const lookupObj = lookupCountry ( country ) ;
104111
@@ -107,11 +114,11 @@ app.get(['/:country/graph', '/graph'], (req, res) => {
107114 }
108115 return getGraph ( { countryCode : lookupObj . iso2 , isCurl } )
109116 . then ( result => res . send ( result ) )
110- . catch ( error => errorHandler ( error , res ) ) ;
117+ . catch ( error => errorHandler ( error , req , res ) ) ;
111118} ) ;
112119
113120app . get ( '/help' , ( req , res ) => {
114- const isCurl = IS_CURL_RE . test ( req . headers [ 'user-agent' ] ) ;
121+ const isCurl = req . isCurl ;
115122 if ( ! isCurl ) {
116123 return res . send ( htmlTemplate ( helpContent ) ) ;
117124 }
@@ -121,23 +128,41 @@ app.get('/help', (req, res) => {
121128
122129app . get ( '/:country' , ( req , res ) => {
123130 const { country } = req . params ;
124- const isCurl = IS_CURL_RE . test ( req . headers [ 'user-agent' ] ) ;
131+ const isCurl = req . isCurl ;
125132 const format = req . query . format ? req . query . format : '' ;
126133 const minimal = req . query . minimal === 'true' ;
127134 const emojis = req . query . emojis === 'true' ;
128- const source = req . query . source ? Number ( req . query . source ) : 1 ;
135+ const source = req . query . source ? Number ( req . query . source ) : 2 ;
129136
130137 if ( ! country || country . toUpperCase ( ) === 'ALL' ) {
131138 if ( format . toLowerCase ( ) === 'json' ) {
132- return getJSONData ( ) . then ( result => {
139+ return getWorldoMetersTable ( { isCurl , emojis , minimal , format } ) . then ( result => {
133140 return res . json ( result ) ;
134- } ) . catch ( error => errorHandler ( error , res ) ) ;
141+ } ) . catch ( error => errorHandler ( error , req , res ) ) ;
142+ }
143+
144+ return getWorldoMetersTable ( { isCurl, emojis, minimal } )
145+ . then ( result => {
146+ return res . send ( result ) ;
147+ } ) . catch ( error => errorHandler ( error , req , res ) ) ;
148+ }
149+ if ( source === 1 ) {
150+ const lookupObj = lookupCountry ( country ) ;
151+
152+ if ( ! lookupObj ) {
153+ return res . status ( 404 ) . send ( countryNotFound ( isCurl ) ) ;
135154 }
155+ const { iso2 } = lookupObj ;
136156
137- return getCompleteTable ( { isCurl, emojis, minimal } )
157+ if ( format . toLowerCase ( ) === 'json' ) {
158+ return getJSONDataForCountry ( iso2 ) . then ( result => {
159+ return res . json ( result ) ;
160+ } ) . catch ( error => errorHandler ( error , req , res ) ) ;
161+ }
162+ return getCountryTable ( { countryCode : iso2 , isCurl, emojis, minimal } )
138163 . then ( result => {
139164 return res . send ( result ) ;
140- } ) . catch ( error => errorHandler ( error , res ) ) ;
165+ } ) . catch ( error => errorHandler ( error , req , res ) ) ;
141166 }
142167
143168 const lookupObj = lookupCountry ( country ) ;
@@ -146,26 +171,12 @@ app.get('/:country', (req, res) => {
146171 return res . status ( 404 ) . send ( countryNotFound ( isCurl ) ) ;
147172 }
148173
149-
150174 const { iso2 } = lookupObj ;
151175
152- if ( source === 2 ) {
153- return getWorldoMetersTable ( { countryCode : iso2 , isCurl, emojis, minimal, format } )
154- . then ( result => {
155- return res . send ( result ) ;
156- } ) . catch ( error => errorHandler ( error , res ) ) ;
157- }
158-
159- if ( format . toLowerCase ( ) === 'json' ) {
160- return getJSONDataForCountry ( iso2 ) . then ( result => {
161- return res . json ( result ) ;
162- } ) . catch ( error => errorHandler ( error , res ) ) ;
163- }
164-
165- return getCountryTable ( { countryCode : iso2 , isCurl, emojis, minimal } )
176+ return getWorldoMetersTable ( { countryCode : iso2 , isCurl, emojis, minimal, format } )
166177 . then ( result => {
167178 return res . send ( result ) ;
168- } ) . catch ( error => errorHandler ( error , res ) ) ;
179+ } ) . catch ( error => errorHandler ( error , req , res ) ) ;
169180} ) ;
170181
171182app . listen ( port , ( ) => console . log ( `Running on ${ port } ` ) ) ;
0 commit comments