-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathapp.js
60 lines (48 loc) · 1.38 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require('express');
const hbs = require("hbs");
const path = require("path");
const app = express();
const weatherData = require('../utils/weatherData');
const port = process.env.PORT || 3000
const publicStaticDirPath = path.join(__dirname, '../public')
const viewsPath = path.join(__dirname, '../templates/views');
const partialsPath = path.join(__dirname, '../templates/partials');
app.set('view engine', 'hbs');
app.set('views', viewsPath);
hbs.registerPartials(partialsPath);
app.use(express.static(publicStaticDirPath));
app.get('', (req, res) => {
res.render('index', {
title: 'Weather App'
})
})
//localhost:3000/weather?address=lahore
app.get('/weather', (req, res) => {
const address = req.query.address
if(!address) {
return res.send({
error: "You must enter address in search text box"
})
}
weatherData(address, (error, {temperature, description, cityName} = {}) => {
if(error) {
return res.send({
error
})
}
console.log(temperature, description, cityName);
res.send({
temperature,
description,
cityName
})
})
});
app.get("*", (req, res) => {
res.render('404', {
title: "page not found"
})
})
app.listen(port, () => {
console.log("Server is up and running on port: ", port);
})