-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (67 loc) · 2.7 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
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
import express from "express";
import axios from "axios";
import dotenv from 'dotenv'
dotenv.config()
const app = express();
app.use(express.static("public"));
app.use(express.urlencoded({extended : true}))
let now = new Date();
let day = now.getDay();
let hours = now.getHours();
let minutes = now.getMinutes();
app.get("/", (req, res)=>{
res.render("index.ejs");
});
app.get("/api/key", (req, res)=>{
res.json({apiKey : process.env.GEOCODING_API_KEYS});
})
app.post("/get-weatherly", async(req, res)=>{
try{
// formatting the address suitable for forwarding this to the api , for eg: spaces with + and removing ,
const address = req.body.address;
// let arr_address = address.split("");
// for(let i = 0; i<arr_address.length; i++){
// if(arr_address[i] === " "){
// arr_address[i]="+";
// }
// else if(arr_address[i] === ","){
// arr_address[i] = "";
// }
// }
// let new_address = arr_address.join("");
// const geo_data = await axios.get(`https://geocode.maps.co/search?q=${new_address}&api_key=${process.env.GEOCODING_API_KEYS}`);
// const lat = geo_data.data[0].lat;
// const lon = geo_data.data[0].lon;
// console.log("latitude and longitude", lat +" "+lon)
const weather_data = await axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${address}&units=metric&appid=${process.env.OPEN_WEATHER_API_KEYS}`);
const temp = weather_data.data.main.temp;
const feelsLike = weather_data.data.main.feels_like;
const iconID = weather_data.data.weather[0].icon;
const weatherDes = weather_data.data.weather[0].description;
const humidity = weather_data.data.main.humidity;
const pressure = weather_data.data.main.pressure;
const name = weather_data.data.name;
const tempMax = weather_data.data.main.temp_max;
const tempMin = weather_data.data.main.temp_min;
// console.log(weatherDes)
const icon_src = (`https://openweathermap.org/img/wn/${iconID}@2x.png`)
res.render("main.ejs", {
c_temp : temp,
c_feelsLike : feelsLike,
c_icon_src : icon_src,
c_weatherDes : weatherDes,
c_humidity : humidity,
c_pressure : pressure,
c_day : day,
c_hours : hours,
c_minutes : minutes,
c_name : name,
c_tempMax : tempMax,
c_tempMin : tempMin,
});
}
catch (error){
console.log("Error :", error);
}
})
app.listen(`${process.env.port}`, console.log(`Server is running on port ${process.env.port}`));