forked from bmorelli25/simple-nodejs-weather-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
33 lines (32 loc) · 1.06 KB
/
Copy pathserver.js
File metadata and controls
33 lines (32 loc) · 1.06 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const app = express();
const apiKey = '********'
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.set('view engine', 'ejs');
app.get('/', function (req, res){
res.render('index', {weather: null, error:null});
});
app.post('/', function(req, res){
var city = req.body.city;
var state = req.body.state;
let url = `http://api.wunderground.com/api/${apiKey}/conditions/q/${state}/${city}.json`;
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["current_observation"] == undefined){
res.render('index', {weather: null, error: 'Error, please try again'});
}
else{
let weather_text = `It's ${weather.current_observation.temp_f} degrees fahrenheit in ${city}.`;
res.render('index', {weather:weather_text, error: null});
}
}
});
});
app.listen(3000);