68 lines
1.4 KiB
JavaScript
68 lines
1.4 KiB
JavaScript
const path = require('path')
|
|
const express = require('express')
|
|
const hbs = require('hbs')
|
|
|
|
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', {
|
|
title: 'Weather',
|
|
name: 'Jason Williams'
|
|
})
|
|
})
|
|
|
|
app.get('/about', (req, res) => {
|
|
res.render('about', {
|
|
title: 'About Me',
|
|
name: 'Jason Williams'
|
|
})
|
|
})
|
|
|
|
app.get('/help', (req, res) => {
|
|
res.render('help', {
|
|
helpText: 'This is some helpful text.',
|
|
title: 'Help',
|
|
name: 'Jason Williams'
|
|
})
|
|
})
|
|
|
|
app.get('/weather', (req, res) => {
|
|
res.send({
|
|
forecast: 'It is snowing. In September.',
|
|
location: 'Calgary'
|
|
})
|
|
})
|
|
|
|
app.get('/help/*', (req, res) => {
|
|
res.render('404', {
|
|
errorMessage: 'Help article not found.',
|
|
title: '404',
|
|
name: 'Jason Williams'
|
|
})
|
|
})
|
|
|
|
app.get('*', (req, res) => {
|
|
res.render('404', {
|
|
errorMessage: 'Page not found.',
|
|
title: '404',
|
|
name: 'Jason Williams'
|
|
})
|
|
})
|
|
|
|
app.listen(4200, () => {
|
|
console.log('Server is up on port 4200.')
|
|
})
|