-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (65 loc) · 1.68 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const express = require('express')
const path = require('path')
const hbs = require('hbs')
const app = express();
const weather = require('./src/getWeather')
const port = process.env.PORT || 3000
//define paths for config express
const publicFileDirectory = path.join(__filename, '../public/');
const viewsPath = path.join(__dirname, 'templates/views');
const partialsPath = path.join(__dirname, 'templates/partials')
//setup handlebars engine and views location
app.set('view engine', 'hbs');
app.set('views', viewsPath)
hbs.registerPartials(partialsPath);
app.use(express.static(publicFileDirectory))
app.get('' , (req, res) => {
res.render('index', {
title: 'Weather',
name : 'mrmr'
})
})
app.get('/help', (req, res) => {
res.render('help', {
title: 'Help',
desc: 'THis some helpful text',
name : 'mrmr'
});
})
app.get('/help/*', (req, res) => {
res.render('404', {
title: '404',
errorMessage: "Help article not found",
name: "mrmr"
})
})
app.get('/about' , (req,res) => {
res.render('about', {
title: 'About me',
name : 'mrmr'
});
})
app.get('/weather' , (req,res) => {
if(!req.query.address){
return res.send({
error: 'You must provide an address'
})
}
weather(req.query.address, (error, response) => {
if(error){
res.send({error})
} else {
res.send({response})
}
})
})
app.get('*', (req, res) => {
res.render('404', {
title: "404",
errorMessage: "page not found",
name: "mrmr"
});
})
app.listen(port, () => {
console.log('server is up in port '+port);
})