-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
58 lines (57 loc) · 1.67 KB
/
script.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
55
56
57
58
const express= require('express');
const https= require('https');
const bodyParser= require('body-parser');
const app= express();
require('dotenv').config();
const apikey= process.env.API_KEY;
app.set('view engine','ejs');
const port= 3000;
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public"));
app.listen((process.env.PORT || port),()=>
{
console.log("server is running on port "+ port);
});
var cityname="Enter city first";
var temp_ture="X";
var descrip="";
var urlimage="";
app.get("/",(req,res)=>
{
const t_day= new Date();
var options={
weekday: "long",
day: "numeric",
month: "long"
};
var day= t_day.toLocaleDateString("en-US",options);
res.render("index",{city:cityname , today: day ,temp: temp_ture,desc: descrip, url:urlimage});
});
app.post("/",(req,res)=>
{
const city = req.body.cname;
const url="https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid="+apikey+"&units=metric";
https.get(url,(respose)=>
{
console.log(respose.statusCode);
if(respose.statusCode==200)
{
respose.on("data",(data)=>
{
const weatherData= JSON.parse(data);
const temperature= weatherData.main.temp;
console.log(temperature);
const descriptions=weatherData.weather[0].description;
cityname=city;
temp_ture=temperature;
descrip=descriptions;
const icons=weatherData.weather[0].icon;
urlimage="https://openweathermap.org/img/wn/"+icons+"@4x.png";
res.redirect("/");
})}
else
{
res.redirect("/");
}
})
})