-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (35 loc) · 1.28 KB
/
app.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
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const exp = require("constants");
const ejs=require("ejs");
const app = express();
app.use(bodyParser.urlencoded({extended: true}))
app.use(express.static("public"));
app.use(express.static("assets"));
app.set('view engine', 'ejs');
app.get("/", function(req, res) {
res.render("index");
});
app.post("/", function(req, res) {
const query = req.body.cityName;
const appKey = "094f27ffffdbfd6b0398b54303c32237";
const units = "metric";
const url = "https://api.openweathermap.org/data/2.5/weather?units="+ units + "&q=" + query + "&appid="+ appKey;
https.get(url, function(response) {
console.log(response.statusCode);
console.log(response.statusMessage);
response.on("data", function(data) {
const weatherData = JSON.parse(data);
temp = weatherData.main.temp;
const weatherDescription = weatherData.weather[0].description;
const icon = weatherData.weather[0].icon;
console.log(icon);
const iconUrl = "http://openweathermap.org/img/wn/"+ icon + "@2x.png";
res.render("index",{temp: temp});
})
})
})
app.listen(3000, function(){
console.log("Server started on port 3000");
})