forked from onclickwebdesign/library-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (47 loc) · 1.55 KB
/
app.js
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
const express = require('express');
const hbs = require('express-handlebars');
const hbsHelpers = require('./views/helpers');
const app = express();
const expressSanitizer = require('express-sanitizer');
const PORT = process.env.PORT || 5001;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(expressSanitizer());
// setup express routes
const mainRoutes = require('./routes');
const booksRoutes = require('./routes/books');
const musicRoutes = require('./routes/music');
const moviesRoutes = require('./routes/movies');
const comicsRoutes = require('./routes/comics');
app.use(mainRoutes);
app.use('/books', booksRoutes);
app.use('/music', musicRoutes);
app.use('/movies', moviesRoutes);
app.use('/comics', comicsRoutes);
// serve static assets in /public directory as /static route
app.use('/static', express.static('public'));
// setup handlebars template engine
app.engine('hbs', hbs.engine({
extname: 'hbs',
defaultLayout: 'main',
layoutsDir: './views/layouts/',
partialsDir: './views/includes/',
helpers: {
if_equal: hbsHelpers.isEqualHelper
}
}));
app.set('view engine', 'hbs');
// 404 route
app.use((req, res, next) => {
const err = new Error(`The requested URL ${req.originalUrl} was not found on this server. That's all we know.`);
err.status = 404;
next(err);
});
// error handler
app.use((err, req, res, next) => {
err.status = err.status || 500;
res.status(err.status).render('error', { err });
});
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});