Skip to content

Instantly share code, notes, and snippets.

@tesh254
Created December 16, 2019 18:15
Show Gist options
  • Save tesh254/b6cc15c5743f8a83f15715d125f9f5ff to your computer and use it in GitHub Desktop.
Save tesh254/b6cc15c5743f8a83f15715d125f9f5ff to your computer and use it in GitHub Desktop.
const axios = require('axios')
const express = require('express')
const { DateTime } = require('luxon')
require('dotenv').config()
const darkSkyApiKey = process.env.DARK_SKY_API_KEY
const gcpKey = process.env.GCP_KEY
const darkSkyBaseUrl = `https://api.darksky.net/forecast`
const address = process.argv.slice(2)
const app = express()
app.get('/location/:place', (req, res) => {
const gcpUrl = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + req.params.place + '&key=' + gcpKey
axios
.get(gcpUrl)
.then(_res => {
const latitude = _res.data.results[0].geometry.location.lat
const longitude = _res.data.results[0].geometry.location.lng
const city = _res.data.results[0].formatted_address
const location = latitude + ',' + longitude
const darkSkyUrl = `${darkSkyBaseUrl}/${darkSkyApiKey}/${location}`
axios
.get(darkSkyUrl)
.then(_res => {
const time = _res.data.currently.time
const formattedTime = DateTime.fromSeconds(time)
.setZone(_res.data.timezone)
.toFormat('DDDD t')
const temperature = _res.data.currently.temperature
_res.send(`It is ${temperature} Fahrenheit (°F) and ${formattedTime} O`clock in ${city}`)
})
.catch(err => {
res.send(`Something went wrong ${err.message}`)
})
})
.catch(err => {
res.send(`Something went wrong ${err.message}`)
})
})
app.listen(4500, () => {
console.log("Server running on port 4500")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment