forked from andrewjmead/node-course-v3-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
102 lines (86 loc) · 2.32 KB
/
app.js
File metadata and controls
102 lines (86 loc) · 2.32 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const path = require('path')
const express = require('express')
const hbs = require('hbs')
const geocode = require('./utils/geocode')
const forecast = require('./utils/forecast')
const app = express()
const port = process.env.PORT || 3000
// Define paths for Express config
const publicDirectoryPath = path.join(__dirname, '../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)
// Setup static directory to serve
app.use(express.static(publicDirectoryPath))
app.get('', (req, res) => {
res.render('index', {
title: 'Weather',
name: 'Andrew Mead'
})
})
app.get('/about', (req, res) => {
res.render('about', {
title: 'About Me',
name: 'Andrew Mead'
})
})
app.get('/help', (req, res) => {
res.render('help', {
helpText: 'This is some helpful text.',
title: 'Help',
name: 'Andrew Mead'
})
})
app.get('/weather', (req, res) => {
if (!req.query.address) {
return res.send({
error: 'You must provide an address!'
})
}
geocode(req.query.address, (error, { latitude, longitude, location } = {}) => {
if (error) {
return res.send({ error })
}
forecast(latitude, longitude, (error, forecastData) => {
if (error) {
return res.send({ error })
}
res.send({
forecast: forecastData,
location,
address: req.query.address
})
})
})
})
app.get('/products', (req, res) => {
if (!req.query.search) {
return res.send({
error: 'You must provide a search term'
})
}
console.log(req.query.search)
res.send({
products: []
})
})
app.get('/help/*', (req, res) => {
res.render('404', {
title: '404',
name: 'Andrew Mead',
errorMessage: 'Help article not found.'
})
})
app.get('*', (req, res) => {
res.render('404', {
title: '404',
name: 'Andrew Mead',
errorMessage: 'Page not found.'
})
})
app.listen(port, () => {
console.log('Server is up on port ' + port)
})