-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (44 loc) · 1.64 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
const express = require('express');
const bodyParse = require('body-parser')
const keys = require('./config/keys.js');
const fetch = require('node-fetch')
const app = express();
//allows express access to static files in 'public' folder
// app.use(express.static('public'))
app.use(express.static(__dirname + '/public'));
//allows us to access JSON body within the HTML req
app.use(bodyParse.urlencoded({extended: false}));
//setting view engine with ejs markup
app.set('view engine', 'ejs');
app.get('/', function(req,res) {
res.render('index', {Weather: null});
});
app.post('/', function(req, res) {
const format = function capitalizeFirstLetter(string) {
result = string.toLowerCase()
result = result.charAt(0).toUpperCase() + result.slice(1);
return result
}
const city = format(req.body.city)
const url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&APPID=${keys.openweatherapi.apikey}&units=imperial`
fetch(url)
.then(response => response.json())
.then(data => {
if(data.main===undefined) {
const error = `Please enter a correct city`
res.render('index', {Weather: error})
}
else {
temperature = Math.round(data.main.temp)
description = data.weather[0].description
const response = `It is currently ${temperature} \u{2109} in ${city}.`
res.render('index', {Weather: response})
}
})
.catch(error => {
console.log(error)
})
})
app.listen(3000,function() {
console.log('App running on port 3000');
});