|
1
|
|
|
const config = require('./config') |
|
2
|
|
|
const express = require('express') |
|
3
|
|
|
const path = require('path') |
|
4
|
|
|
const logger = require('./config/logger') |
|
5
|
|
|
const morgan = require('morgan') |
|
6
|
|
|
const session = require('express-session') |
|
7
|
|
|
const cookieParser = require('cookie-parser') |
|
8
|
|
|
const methodOverride = require('method-override') |
|
9
|
|
|
const bodyParser = require('body-parser') |
|
10
|
|
|
const flash = require('connect-flash') |
|
11
|
|
|
const mongoose = require('mongoose') |
|
12
|
|
|
|
|
13
|
|
|
const app = express() |
|
14
|
|
|
|
|
15
|
|
|
require('./models')(app) |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* API keys and Passport configuration. |
|
19
|
|
|
*/ |
|
20
|
|
|
const passport = require('passport') |
|
21
|
|
|
require('./config/passport') |
|
22
|
|
|
|
|
23
|
|
|
// connect to Mongo when the app initializes |
|
24
|
|
|
mongoose.connect(config.db) |
|
25
|
|
|
|
|
26
|
|
|
// view engine setup |
|
27
|
|
|
app.set('views', path.join(__dirname, 'views')) |
|
28
|
|
|
app.set('view engine', 'pug') |
|
29
|
|
|
|
|
30
|
|
|
// uncomment after placing your favicon in /public |
|
31
|
|
|
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); |
|
32
|
|
|
if (config.env === 'development') { |
|
33
|
|
|
app.use(morgan('dev', { stream: { write: message => logger.info(message) } })) |
|
34
|
|
|
} else { |
|
35
|
|
|
app.use(morgan('combined', { stream: { write: message => logger.info(message) } })) |
|
36
|
|
|
} |
|
37
|
|
|
app.use(bodyParser.json()) |
|
38
|
|
|
app.use(bodyParser.urlencoded({ extended: true })) |
|
39
|
|
|
app.use(methodOverride(function(req, res){ |
|
|
|
|
|
|
40
|
|
|
if (req.body && typeof req.body === 'object' && '_method' in req.body) { |
|
|
|
|
|
|
41
|
|
|
logger.info('In method override') |
|
42
|
|
|
// look in urlencoded POST bodies and delete it |
|
43
|
|
|
var method = req.body._method |
|
44
|
|
|
delete req.body._method |
|
45
|
|
|
return method |
|
46
|
|
|
} |
|
47
|
|
|
})) |
|
48
|
|
|
app.use(cookieParser()) |
|
49
|
|
|
app.use(session({ |
|
50
|
|
|
secret: config.cookieSecret, |
|
51
|
|
|
name: config.sessionName, |
|
52
|
|
|
proxy: true, |
|
53
|
|
|
resave: true, |
|
54
|
|
|
saveUninitialized: true |
|
55
|
|
|
})) |
|
56
|
|
|
app.use(flash()) |
|
57
|
|
|
app.use('/static', express.static(path.join(__dirname, 'public'))) |
|
58
|
|
|
app.use(passport.initialize()) |
|
59
|
|
|
app.use(passport.session()) |
|
60
|
|
|
require('./config/routes')(app) |
|
61
|
|
|
|
|
62
|
|
|
// catch 404 and forward to error handler |
|
63
|
|
|
app.use(function (req, res, next) { |
|
64
|
|
|
var err = new Error('Not Found') |
|
65
|
|
|
err.status = 404 |
|
66
|
|
|
next(err) |
|
67
|
|
|
}) |
|
68
|
|
|
|
|
69
|
|
|
// error handler |
|
70
|
|
|
app.use(function (err, req, res, next) { |
|
|
|
|
|
|
71
|
|
|
// set locals, only providing error in development |
|
72
|
|
|
res.locals.message = err.message |
|
73
|
|
|
res.locals.error = req.app.get('env') === 'development' ? err : {} |
|
74
|
|
|
logger.error(err.stack) |
|
75
|
|
|
// render the error page |
|
76
|
|
|
res.status(err.status || 500) |
|
77
|
|
|
res.render('error') |
|
78
|
|
|
}) |
|
79
|
|
|
|
|
80
|
|
|
module.exports = app |
|
81
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.