@@ -2,17 +2,21 @@ const express = require('express');
22const app = express ( ) ;
33const lookup = require ( 'country-code-lookup' ) ;
44const morgan = require ( 'morgan' ) ;
5+ const stripAnsi = require ( 'strip-ansi' ) ;
56
67const port = process . env . PORT || 3001 ;
78
89const { getCountryTable, getJSONData, getJSONDataForCountry } = require ( './lib/byCountry' ) ;
910const { getCompleteTable } = require ( './lib/corona' ) ;
10- const { countryUpperCase , lookupCountry } = require ( './lib/helpers' ) ;
11+ const { lookupCountry } = require ( './lib/helpers' ) ;
1112
1213
1314function errorHandler ( error , res ) {
1415 console . error ( error ) ;
15- return res . send ( 'I am sorry. Something went wrong. Please report it' ) ;
16+ return res . send ( `
17+ I am sorry. Something went wrong. Please report it \n
18+ ${ error . message }
19+ ` ) ;
1620}
1721
1822app . use ( morgan ( ':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms' ) ) ;
@@ -21,37 +25,54 @@ app.use((req, res, next) => {
2125 next ( ) ;
2226} ) ;
2327
28+
29+ + app . get ( '/updates' , ( req , res ) => {
30+ const format = req . query . format ? req . query . format : '' ;
31+ if ( format . toLowerCase ( ) === 'json' ) {
32+ return getLiveUpdates ( true ) . then ( result => {
33+ return res . json ( result ) ;
34+ } ) . catch ( error => errorHandler ( error , res ) ) ;
35+ }
36+ return getLiveUpdates ( false ) . then ( result => {
37+ return res . send ( result ) ;
38+ } ) . catch ( error => errorHandler ( error , res ) ) ;
39+ } ) ;
40+
2441app . get ( '/' , ( req , res ) => {
2542 const isCurl = req . headers [ 'user-agent' ] . match ( / \b c u r l \b / gmi) !== null ;
2643 const format = req . query . format ? req . query . format : '' ;
44+ const minimal = req . query . minimal === 'true' ? true : false ;
45+ const emojis = req . query . emojis === 'true' ? true : false ;
2746
2847 if ( format . toLowerCase ( ) === 'json' ) {
2948 return getJSONData ( ) . then ( result => {
3049 return res . json ( result ) ;
3150 } ) . catch ( error => errorHandler ( error , res ) ) ;
3251 }
3352
34- return getCompleteTable ( { isCurl } ) . then ( result => {
35- return res . send ( result ) ;
36- } ) . catch ( error => errorHandler ( error , res ) ) ;
53+ return getCompleteTable ( { isCurl, emojis, minimal } )
54+ . then ( result => {
55+ return res . send ( result ) ;
56+ } ) . catch ( error => errorHandler ( error , res ) ) ;
3757} ) ;
3858
3959app . get ( '/:country' , ( req , res ) => {
4060 const { country } = req . params ;
4161 const isCurl = req . headers [ 'user-agent' ] . match ( / \b c u r l \b / gmi) !== null ;
4262 const format = req . query . format ? req . query . format : '' ;
43-
63+ const minimal = req . query . minimal === 'true' ? true : false ;
64+ const emojis = req . query . emojis === 'true' ? true : false ;
4465 if ( ! country || 'ALL' === country . toUpperCase ( ) ) {
4566 if ( format . toLowerCase ( ) === 'json' ) {
4667 return getJSONData ( ) . then ( result => {
4768 return res . json ( result ) ;
4869 } ) . catch ( error => errorHandler ( error , res ) ) ;
4970 }
5071
51-
52- return getCompleteTable ( { isCurl } ) . then ( result => {
53- return res . send ( result ) ;
54- } ) . catch ( error => errorHandler ( error , res ) ) ;
72+ return getCompleteTable ( { isCurl , emojis , minimal } )
73+ . then ( result => {
74+ return res . send ( result ) ;
75+ } ) . catch ( error => errorHandler ( error , res ) ) ;
5576 }
5677
5778 let lookupObj = lookupCountry ( country ) ;
@@ -63,7 +84,7 @@ app.get('/:country', (req, res) => {
6384 Ex:
6485 - /UK: for United Kingdom
6586 - /US: for United States of America.
66- - /India : for India .
87+ - /Italy : for Italy .
6788 ` ) ;
6889 }
6990
@@ -75,9 +96,10 @@ app.get('/:country', (req, res) => {
7596 } ) . catch ( error => errorHandler ( error , res ) ) ;
7697 }
7798
78- return getCountryTable ( { countryCode : iso2 , isCurl } ) . then ( result => {
79- return res . send ( result ) ;
80- } ) . catch ( error => errorHandler ( error , res ) ) ;
99+ return getCountryTable ( { countryCode : iso2 , isCurl, emojis, minimal } )
100+ . then ( result => {
101+ return res . send ( result ) ;
102+ } ) . catch ( error => errorHandler ( error , res ) ) ;
81103} ) ;
82104
83105
0 commit comments