-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
33 lines (29 loc) · 1.07 KB
/
weather.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
const express=require("express");
const bodyParser=require("body-parser");
const app=express();
app.use(bodyParser.urlencoded({extended:true}));
const https=require("https");
app.get("/",function(req,res){
res.sendFile(__dirname+"/index.html");
});
app.post("/",function(req,res){
var query=req.body.city;
var appid="0f197a559b4cd0d7f6a06150b21e014d";
var unit=req.body.temp;
var url="https://api.openweathermap.org/data/2.5/weather?appid="+appid+"&q="+query+"&units="+unit;
https.get(url,function(response){
response.on("data",function(data){
var weatherData=JSON.parse(data);
var temp=weatherData.main.temp;
var desc=weatherData.weather[0].description;
var icon=weatherData.weather[0].icon;
res.write("<p>The description of the weather is "+desc+"</p>");
res.write("<h1>The temperature in "+query+" is "+temp+" degrees Celcius.</h1>");
res.write("<img src=http://openweathermap.org/img/wn/"+icon+"@2x.png></img>");
res.send();
});
});
});
app.listen(3000,function(){
console.log("Server started at port 3000.");
});