-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
106 lines (92 loc) · 2.4 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/** Express router providing user related configuration
* @module routers/users
* @requires express
* @requires favicon
* @requires cookieParser
* @requires bodyParser
* @requires hbs
* @requires index
*/
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const hbs = require('express-handlebars');
const index = require('./routes/index');
const app = express();
/**
* Method to configurate template engine
* @name engine
* @function
* @memberof module:routers/users
* @inner
* @param {string} setting - handlebars
*/
app.engine(
'hbs',
hbs({
extname: 'hbs',
defaultLayout: 'layout',
layoutsDir: __dirname + '/views/layouts/'
})
);
/**
* Method to set views folder
* @name set
* @function
* @memberof module:routers/users
* @inner
* @param {string} views - __dirname, 'views'
*/
app.set('views', path.join(__dirname, 'views'));
/**
* Method to set views engine
* @name set
* @function
* @memberof module:routers/users
* @inner
* @param {string} view engine - 'view engine', 'hbs' -> handlebars
*/
app.set('view engine', 'hbs');
/** app.use favicon,bodyParser to handle post method, static files, etc */
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
/**
* Method to handle error
* catch 404 and forward to error handler
* @name use
* @function
* @memberof module:routers/users
* @inner
* @param {function} function - catch 404 error
*/
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/**
* Method to handle errors
* and render
* @name use
* @function
* @memberof module:routers/users
* @inner
* @param {function} function -set locals, only providing error in development,Render the error Page
*/
// error handler
app.use(function(err, req, res, next) {
/** set locals, only providing error in development */
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
/** Render the error Page */
res.status(err.status || 500);
res.render('error');
});
module.exports = app;