-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
37 lines (29 loc) · 1.18 KB
/
Copy pathapp.js
File metadata and controls
37 lines (29 loc) · 1.18 KB
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
const express = require("express");
const https = require("https");
const bp = require("body-parser");
const app = new express();
app.use(bp.urlencoded({ extended: true }));
app.get("/",function(req,res){
res.sendFile(__dirname + "/index.html");
});
app.post("/",function(req,res){
const query = req.body.city , unit = "metric";
const appid = process.env.appId;
const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&appid=" + appid + "&units=" + unit ;
https.get(url,function(response){
response.on("data",function(data){
const weatherData = JSON.parse(data);
const temp = weatherData.main.temp;
const weatherdesc = weatherData.weather[0].description;
const icon = weatherData.weather[0].icon;
const imageurl = "http://openweathermap.org/img/wn/" + icon + "@2x.png";
res.write("<p>The Weather is currently " + weatherdesc + "</p>");
res.write("<h1>The Temperature in " + query + " is " + temp + " Degree Celcius</h1>");
res.write("<img src = \"" + imageurl + "\" alt = \"" + weatherdesc + "\">");
res.send();
});
});
});
app.listen(3000,function(){
console.log("server is running in port 3000");
});