|
| 1 | +const express = require('express') |
| 2 | +const axios = require('axios') |
| 3 | +const port = process.env.PORT || 3001 |
| 4 | + |
| 5 | +// Configure app to use bodyParser to parse json data |
| 6 | +const app = express() |
| 7 | + |
| 8 | +const server = require('http').createServer(app) |
| 9 | +require('dotenv').config() |
| 10 | + |
| 11 | +// custom HTTP headers for authenticating requests sent to Algolia places server |
| 12 | +const HEADERS = { |
| 13 | + 'X-Algolia-Application-Id': process.env.ALGOLIA_PLACES_APP_ID || '', |
| 14 | + 'X-Algolia-API-Key': process.env.ALGOLIA_PLACES_API_KEY || '', |
| 15 | +} |
| 16 | +const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY |
| 17 | + |
| 18 | +// Test server is working (GET http://localhost:3001/) |
| 19 | +app.get('/', function (req, res) { |
| 20 | + res.send(` |
| 21 | + <!DOCTYPE html> |
| 22 | + <html lang="en"> |
| 23 | + <head> |
| 24 | + <meta charset="UTF-8"/> |
| 25 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"/> |
| 26 | + <title>Weather React API</title> |
| 27 | + </head> |
| 28 | + <body> |
| 29 | + <h1>Welcome to <a href="https://iamsainikhil.github.io/weather-react" target="_blank" rel="noreferrer noopener">Weather React</a> application's proxy server</h1> |
| 30 | + </body> |
| 31 | + </html> |
| 32 | + `) |
| 33 | +}) |
| 34 | + |
| 35 | +// Fetch address based on latlong |
| 36 | +app.get('/address/coords/:latlong', (req, res) => { |
| 37 | + const {latlong} = req.params |
| 38 | + const url = `https://places-dsn.algolia.net/1/places/reverse?aroundLatLng=${latlong},&hitsPerPage=1&language=en` |
| 39 | + axios |
| 40 | + .get(url, {headers: HEADERS}) |
| 41 | + .then((response) => { |
| 42 | + const {data} = response |
| 43 | + res.status(200) |
| 44 | + res.json(data) |
| 45 | + }) |
| 46 | + .catch((err) => { |
| 47 | + res.status(err.response ? err.response.status : 500) |
| 48 | + res.send(err.message || 'Something went wrong! Please try again later.') |
| 49 | + }) |
| 50 | +}) |
| 51 | + |
| 52 | +// Fetch weather forecast based on latlong |
| 53 | +app.get('/forecast/coords/:latlong', (req, res) => { |
| 54 | + const {latlong} = req.params |
| 55 | + const url = `https://api.darksky.net/forecast/${DARKSKY_API_KEY}/${latlong}?extend=hourly&exclude=minutely,flags` |
| 56 | + axios |
| 57 | + .get(url) |
| 58 | + .then((response) => { |
| 59 | + const {data} = response |
| 60 | + res.status(200) |
| 61 | + res.json(data) |
| 62 | + }) |
| 63 | + .catch((err) => { |
| 64 | + res.status(err.response ? err.response.status : 500) |
| 65 | + res.send(err.message || 'Something went wrong! Please try again later.') |
| 66 | + }) |
| 67 | +}) |
| 68 | + |
| 69 | +// Fetch address list based on query |
| 70 | +app.get('/places/query/:city/:latlong', (req, res) => { |
| 71 | + const {city, latlong} = req.params |
| 72 | + axios |
| 73 | + .request({ |
| 74 | + url: 'https://places-dsn.algolia.net/1/places/query', |
| 75 | + method: 'post', |
| 76 | + data: { |
| 77 | + query: city, |
| 78 | + type: 'city', |
| 79 | + aroundLatLng: latlong, |
| 80 | + }, |
| 81 | + headers: HEADERS, |
| 82 | + }) |
| 83 | + .then((response) => { |
| 84 | + const {data} = response |
| 85 | + res.status(200) |
| 86 | + res.json(data) |
| 87 | + }) |
| 88 | + .catch((err) => { |
| 89 | + res.status(err.response ? err.response.status : 500) |
| 90 | + res.send(err.message || 'Something went wrong! Please try again later.') |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +// Start the server |
| 95 | +server.listen(port) |
| 96 | +console.log('Server is listening on port ' + port) |
0 commit comments