-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
160 lines (114 loc) · 4.55 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var request = require('request');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}))
// default city
var city = 'Allahabad';
app.use(bodyParser.json());
// rendering home page
app.get('/',function (req,res) {
var url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&appid=41898b7881fc62d5a199b9f3f54b3b38`;
request(url , function (error , respone , body) {
var weather_json = JSON.parse(body);
var datetime = new Date().toDateString();
// console.log(weather_json);
var data = {
city: city,
temp: Math.round(weather_json.main.temp),
icon: weather_json.weather[0].icon,
des: weather_json.weather[0].description,
humi: weather_json.main.humidity,
country: weather_json.sys.country,
wind: weather_json.wind.speed,
temp_max: weather_json.main.temp_max,
pressure: weather_json.main.pressure,
date: datetime
}
var data = { data: data }
res.render('weather.ejs', data);
});
});
// post request from search input from weather.ejs
app.post('/weather', (req,res)=>{
var city = req.body.city2;
res.redirect('/weather2/'+city);
})
// post request from the celsius page
app.post('/weather2', (req,res)=>{
var city = req.body.city;
res.redirect('/weather2/'+ city);
})
// finding data of the searched
app.get('/weather2/:city', function (req, res) {
var city = req.params.city;
var url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&appid=76c8fdc8db07e476c13e988cb7a5690d`;
request(url, function (error, respone, body) {
var weather_json = JSON.parse(body);
//console.log(weather_json);
if (!weather_json.message)
{
var datetime = new Date().toDateString();
var cel = ((Math.round(weather_json.main.temp) - 32) * 5) / 9;
var data = {
city: weather_json.name,
temp: Math.round(weather_json.main.temp),
icon: weather_json.weather[0].icon,
des: weather_json.weather[0].description,
humi: weather_json.main.humidity,
country : weather_json.sys.country,
wind: weather_json.wind.speed,
temp_max: weather_json.main.temp_max,
pressure: weather_json.main.pressure,
date: datetime
}
var data = { data: data }
res.render('weather.ejs', data);
}else{
res.render('weather.ejs', { data : "city not found" });
}
});
});
// taking city name to convert into celsius
app.post('/cel', function (req, res) {
var city = req.body.city;
res.redirect('/cel/'+ city)
});
// converting to celsius
app.get('/cel/:city', function (req, res) {
var city = req.params.city;
//console.log(city);
var url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&appid=41898b7881fc62d5a199b9f3f54b3b38`;
request(url, function (error, respone, body) {
var weather_json = JSON.parse(body);
//console.log(weather_json);
if (!weather_json.message) {
var datetime = new Date().toDateString();
var cel = ((Math.round(weather_json.main.temp) - 32) * 5) / 9;
var max = ((Math.round(weather_json.main.temp_max) - 32) * 5) / 9;
var km = ((weather_json.wind.speed) * 1.6093440)
var kmp = km.toFixed(3);
var data = {
city: weather_json.name,
temp: Math.round(cel),
icon: weather_json.weather[0].icon,
des: weather_json.weather[0].description,
humi: weather_json.main.humidity,
country: weather_json.sys.country,
wind: kmp,
temp_max: Math.round(max),
pressure: weather_json.main.pressure,
date: datetime,
cel : "yes"
}
var data = { data: data }
res.render('weather.ejs', data);
} else {
res.render('weather.ejs', { data: "city not found" });
}
});
});
app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});