-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
77 lines (58 loc) · 1.56 KB
/
Copy pathapp.js
File metadata and controls
77 lines (58 loc) · 1.56 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
const path = require('path')
const request = require('request')
const express = require('express')
const hbs = require('hbs')
const geocode = require('./utils/geocode')
const forecast = require('./utils/forecast')
const app = express()
// 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', {
})
})
app.get('/about', (req, res) => {
res.render('about', {
})
})
app.get('/help', (req, res) => {
res.render('help', {
})
})
app.get('/home', (req, res) => {
res.render('home', {
})
})
app.get('/weather', (req, res) => {
if(!req.query.address){
return res.send({
error:'You must provide a search term '
})
}
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.listen(3000, () => {
console.log('Server is up on port 3000.')
})