-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
82 lines (69 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser');
const axios = require('axios');
const kelvinToCelsius = require('kelvin-to-celsius');
// API base URL
const BASE_URL = "http://api.openweathermap.org/data/2.5/weather?"
// API key
const API_KEY = "99fd063b248c10d275944af7307bc5a6"
const SendWeatherRequest = async (city) => {
var clientServerOptions = {
uri: BASE_URL + "q=" + city + "&appid=" + API_KEY,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}
try {
const resp = await axios.get(clientServerOptions.uri, {
headers: clientServerOptions.headers
});
return resp.data;
} catch (err) {
// Handle Error Here
console.error(err);
}
};
// Create express app
const app = express()
const port = 3456
app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.raw());
app.get('/', (req, res) => {
var response = {
fistname: 'Enes',
lastname: 'Turan'
}
res.json(response)
})
app.get('/enes', (req, res) => {
var response = {
message: 'Ahmed Enes Turan'
}
res.json(response)
})
app.get('/temperature', async (req, res, next) => {
try {
const cityname = req.query.city;
const weatherData = await SendWeatherRequest(cityname)
let rawTempData = weatherData.main.temp;
// Convert Kelvin to Celsius
let celsiusData = kelvinToCelsius(rawTempData);
let coords = weatherData.coord;
var response = {
temperature: celsiusData,
// city: cityname,
// coords: coords
}
res.status(200).json(response);
} catch (error) {
res.status(404).json(error);
next(error)
}
})
app.listen(port, () => {
console.log(`Best Cloud Academy Case Study App listening at http://localhost:${port}`)
})