-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
44 lines (32 loc) · 1.25 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
34
35
36
37
38
39
40
41
42
43
44
import express from "express";
import path from "path";
import https from "https";
import pkg from "dotenv";
pkg.config();
const app = express();
app.use(express.urlencoded({extended: true}));
const __dirname = path.resolve();
app.get("/", (req, res)=>{
res.sendFile(__dirname+"/index.html");
});
const apiKey = process.env.API_KEY;
const unit = "metric";
app.post("/", (req, res)=>{
let city = req.body.city;
let apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=" + unit + "&appid="+apiKey;
https.get(apiUrl, (response)=>{
response.on("data", (data)=>{
let weatherObj = JSON.parse(data);
let description = weatherObj.weather[0].description;
let iconUrl = "http://openweathermap.org/img/wn/" + weatherObj.weather[0].icon + "@2x.png";
let temperature = weatherObj.main.temp;
res.write("<p>The weather condition is " + description +"</p>");
res.write("<h1>Temperature in "+ city +" is "+ temperature +" degree celcius</h1>");
res.write("<img src=" + iconUrl + ">");
res.send();
});
});
});
app.listen(3000, ()=>{
console.log("server is deployed on 3000 port");
});