-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (42 loc) · 1.62 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const app = express()
// this is the api key
const apiKey = '**************************';
var retro;
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs')
app.get('/', function (req, res) {
res.render('index', {weather: null, error: null});
})
app.post('/', function (req, res) {
let city = req.body.city;
let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`
request(url, function (err, response, body) {
if(err){
res.render('index', {weather: null, error: 'Error, please try again'});
} else {
let weather = JSON.parse(body)
if(weather.main == undefined){
res.render('index', {weather: null, error: 'Error, please try again'});
} else {
// used to get clouds and all stuff
let wc = weather.clouds.all;
res.render('index', {weather: weather.main.temp, wm: weather.weather[0].main, wd: weather.weather[0].description, wi: weather.weather[0].icon, wp: weather.main.pressure, wh: weather.main.humidity, wmax: weather.main.temp_max, wmin: weather.main.temp_min, wc: wc, city: city, error: null});
}
}
});
})
app.listen(3000, function () {
console.log('Weather-App listening on port 3000!')
})
function titleCase(str){
str = str.toLowerCase().split(' ');
let final = [ ];
for(let word of str){
final.push(word.charAt(0).toUpperCase()+ word.slice(1));
}
return final.join(' ')
}