1
0
Fork 0
CompleteNodeJS/web-server/src/app.js

103 lines
2.2 KiB
JavaScript

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 || 4200
// 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) => {
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: error })
}
forecast(latitude, longitude, (error, forecastData) => {
if (error) {
return res.send({ error: 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', {
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(port, () => {
console.log('Server is up on port ' + port + '.')
})