-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
40 lines (38 loc) · 1.19 KB
/
app.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
const express = require('express')
const app=express()
const port=process.env.PORT || 3000;
const request = require('request')
const dotenv=require('dotenv').config()
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({extended:true}))
let location=""
app.use(express.static(__dirname + '/views'))
app.set("view engine","ejs")
//input form
app.post('/run',function(req,res){
location=req.body.place
res.redirect('/foam')
});
app.get('/', function (req,res){
res.render("initial")
})
// requesting the data from API
app.get('/foam',function (req,res){
request('https://api.openweathermap.org/data/2.5/weather?q='+location+'&units=metric&APPID='+process.env.WEATHER_KEY,function(error,response,body){
if(!error && response.statusCode==200){
let newbody=JSON.parse(body)
let data={
"temp":newbody.main.temp,
"description":newbody.weather[0].description,
"humidity":newbody.main.humidity,
"pressure":newbody.main.pressure,
"name":newbody.name+', '+newbody.sys.country
};
res.render('result',{data:data})
}
else{
res.render('error')
}
})
})
app.listen(port, () => console.log("app listening on port 3000"))