udemy-node-web-server/server.js

58 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-11-28 03:00:19 +00:00
const express = require('express');
const port = process.env.PORT || 8888;
const hbs = require('hbs');
const fs = require('fs');
var app = express();
hbs.registerPartials(`${__dirname}/views/partials/`);
app.set('view engine', 'hbs');
hbs.registerHelper('getCurrentYear', () => {
return new Date().getFullYear();
});
//middelware
app.use((req, res, next) => {
var now = new Date().toString();
var log = `${now}: ${req.method} ${req.url}`;
console.log(log);
fs.appendFile('server.log', `${log}\n`, (err) => {
if (err) {
console.log("Unable to append to server.log.");
}
})
next();
});
// app.use((req, res, next) => {
// res.render('maintenance.hbs')
// });
app.use(express.static(`${__dirname}/public`));
hbs.registerHelper('screamIt', (text) => {
return text.toUpperCase();
})
app.get('/', (req, res) => {
res.render('home.hbs', {
title: "Tyrel's Page",
welcomeMessage: "butts",
})
});
app.get('/about', (req, res) => {
res.render('about.hbs', {
title: "About Page",
})
})
app.get('/bad', (req, res) => {
res.send({
errorMessage: 'FAILURE'
})
})
app.listen(port, () => {
console.log(`Server is listening at http://127.0.0.1:${port}/`);
})