-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (36 loc) · 1.64 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
51
52
53
54
const express = require("express");
const https = require("https");
const bodyparser = require("body-parser");
const app = express();
app.use(bodyparser.urlencoded({extended:true}));
app.get("/", (req,res)=>{
res.sendFile(__dirname+"/index.html");
})
app.post("/", (req,res)=>{
const city=req.body.cityName;
const api="9ad92fe0aa71d0aa442d8327623b2ecb";
const units=req.body.units;
const url= "https://api.openweathermap.org/data/2.5/weather?appid="+api+"&q="+city+"&units="+units;
https.get( url, (response)=>{
response.on('data', (d) => {
const ResponseData = JSON.parse(d);
if(ResponseData.cod=="200"){
const weatherTemp = ResponseData.main.temp;
const weatherDesc = ResponseData.weather[0].description;
const iconUrl = "http://openweathermap.org/img/wn/"+ ResponseData.weather[0].icon +"@2x.png";
var unitName ;
units=="metric"? unitName="degrees celcius":(units=="imperial"?unitName="degrees fahrenheit":unitName="kelvin");
res.write("<p>Weather is currently "+weatherDesc +"</p>");
res.write("<h1>Temprature in "+city+" is "+ weatherTemp+" "+unitName +"</h1>");
res.write("<img src="+iconUrl+">");
res.send();
}
else{
res.write(ResponseData.message);
res.write(". Please try again");
res.send();
}
});
})
})
app.listen(process.env.PORT || 3000, ()=> console.log(`Server started on port : ${process.env.PORT || 3000}`));