-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (36 loc) · 1.5 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
const express = require("express");
const https = require("https");
const bodyParser=require("body-parser")
const { rawListeners } = require("process");
const { KeyObject } = require("crypto");
const app=express();
const imageUrl="http://openweathermap.org/img/wn/[email protected]";
app.use(bodyParser.urlencoded({extended:true}));
app.get("/",function(req,res){
res.sendFile(__dirname+"/index.html")
});
app.post("/",function(req,res){
const cityName=req.body.cityName;
const url="https://api.openweathermap.org/data/2.5/weather?units=metric&appid=8c64212b6486efd6c3d0c6ae61e872e3&q="+cityName;
https.get(url,function(response){
response.on("data",function(data){
const weatherData=JSON.parse(data);
const WeatherCode=weatherData.cod;
if(WeatherCode==404){
res.write("<h1>City Not found</h1>");
res.send();
}
else{
const icon=weatherData.weather[0].icon;
const imageUrl="http://openweathermap.org/img/wn/"+ icon+"@2x.png";
res.write("<h1>The weather condition at "+ weatherData.name+" is "+weatherData.weather[0].description+"</h1>");
res.write("<h2>Current temp : "+weatherData.main.temp+"</h2>");
res.write("<image src="+ imageUrl +">")
res.send();
}
})
})
})
app.listen(3000,function(){
console.log("Server started at port 3000");
});