-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
105 lines (81 loc) · 2.33 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
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const passport = require('./config/passport');
const models = require('./models');
const compression = require('compression')
const methodOverride = require('method-override');
const config = require('./config/config.json');
const app = express();
/*
Settings
*/
app.set('port', 3030);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
Object.keys(config.settings).forEach(keys => {
let element = config.settings[keys];
app.set(keys, element);
})
console.log(app);
app.use(compression());
/*
Middlewares
*/
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride(function (req) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
var method = req.body._method;
delete req.body._method;
return method;
}
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//- -- session middlewares
const session = require('express-session');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
app.use(session({
secret: "It's cat opposite of computer",
store: new SequelizeStore({
db: models.sequelize,
checkExpirationInterval: 15 * 60 * 1000,
expiration: 24 * 60 * 60 * 1000
}),
resave: false,
saveUninitialized: true
}));
//- -- auth midllewares
app.use(passport.initialize());
app.use(passport.session());
//- -- router middllewares
app.get('/testform', (req, res, next) => {
res.json(app.locals)
})
app
.post('/testform', (req, res, next) => {
res.json(req.body);
});
app
.use('/admin', require('./controllers/users.controller').isAuth, require('./routes/admin'));
app
.use('/', require('./routes/main'));
//- error handler middlewares
// catch 404 and forward to error handler
app.use((req, res, next) => {
let err = new Error('Not Found');
err.status = 404;
next(err);
})
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
})
module.exports = app;