11const express = require ( 'express' )
22const weather = require ( './src/weatherAPI.js' )
3- const { requestCityFromIP , requestCoordsFromIP } = require ( './src/geoipAPI.js' )
3+ const { geolocateFromIP } = require ( './src/geoipAPI.js' )
44const anonymizeip = require ( './src/anonymizeip.js' )
55var path = require ( 'path' ) ;
66
@@ -28,15 +28,16 @@ api.get('/', (req, res) => {
2828 ipAddr : anonymizeip ( clientIP ) ,
2929 }
3030
31- requestCityFromIP ( clientIP ) . then ( ( coords ) => {
32- console . log ( 'Coords obj:' , coords )
31+ const weatherResp = geolocateFromIP ( clientIP ) . then ( ( coords ) => {
32+ // If the geolocation is successful, format the name of the returned location,
33+ // then call the weather API with the coordinates and timezone.
3334
3435 if ( "regionName" in coords && "city" in coords && "country" in coords ) {
3536 renderValues . locationName = `${ coords . city } , ${ coords . regionName } , ${ coords . country } `
3637 } else if ( "country" in coords ) {
3738 if ( "city" in coords ) {
3839 renderValues . locationName = `${ coords . city } , ${ coords . country } `
39- } else if ( "region " in coords ) {
40+ } else if ( "regionName " in coords ) {
4041 renderValues . locationName = `${ coords . regionName } , ${ coords . country } `
4142 } else {
4243 renderValues . locationName = coords . country
@@ -47,12 +48,27 @@ api.get('/', (req, res) => {
4748 renderValues . locationName = coords . regionName
4849 }
4950
50- // By default, display the weather in Toronto.
51- return weather ( "43.7001" , "-79.4163" , "America/Toronto" )
52- } ) . then ( ( weatherData ) => {
53- // renderValues.forecast = JSON.stringify(weatherData)
51+ return weather ( coords . lat , coords . lon , coords . timezone )
52+ } ) . catch ( ( coords ) => {
53+ // If the geolocation fails, default to Toronto, Ontario, Canada, then call
54+ // the weather API with the coordinates and timezone.
55+
56+ renderValues . locationName = "Toronto, Ontario, Canada"
57+ return weather ( "43.7" , "-79.42" , "America/Toronto" )
58+ } )
59+
60+ weatherResp . then ( ( weatherData ) => {
61+ // Once the weather API call is successful, render the index page with the
62+ // template values specified in `renderValues`.
63+
5464 renderValues . forecast = weatherData
5565 res . render ( 'index' , renderValues )
66+ } ) . catch ( ( e ) => {
67+ // If the weather API call fails, render the index page with the template
68+ // and the limited values that are available.
69+
70+ console . error ( "Error in main route:" , e )
71+ res . render ( 'index' , renderValues )
5672 } )
5773} )
5874
@@ -61,19 +77,24 @@ api.get('/geolocate', (req, res) => {
6177 // will send a request to `/geolocate` to get the estimated coordinates
6278 // of the client's IP address. This will then return the coordinates to the
6379 // client, which will use them to call the weather API as it normally would.
64- geoip ( req . ip ) . then ( ( coords ) => {
80+ geolocateFromIP ( req . ip ) . then ( ( coords ) => {
6581 res . json ( coords )
82+ } ) . catch ( ( e ) => {
83+ res . json ( { status : 'error' , code : 500 , message : e . message } )
6684 } )
6785} )
6886
6987api . get ( '/weather' , ( req , res ) => {
7088 const queryParams = req . query
7189 if ( ! queryParams . lat || ! queryParams . lon || ! queryParams . timezone ) {
7290 res . status ( 400 ) . send ( 'Missing query parameters. All of the following are required: lat, lon, timezone' )
91+ return
7392 }
7493
7594 weather ( queryParams . lat , queryParams . lon , queryParams . timezone ) . then ( ( weatherData ) => {
7695 res . json ( weatherData )
96+ } ) . catch ( ( e ) => {
97+ res . json ( { status : 'error' , code : 500 , message : e . message } )
7798 } )
7899} )
79100
0 commit comments