-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
64 lines (53 loc) · 1.62 KB
/
index.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
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const bodyParser = require('body-parser');
const path = require('path');
const rootPath = __dirname;
const config = require('config');
app.set('port', config.port);
app.set('view engine', 'pug');
app.set('views', path.join(rootPath, './views'));
app.use('/', express.static(path.join(rootPath, '/public')));
/**models**/
require('./config/database.js');
require('./models/log');
require('./models/user');
/**Application wide middlewares**/
require('./config/session.js').addSession(app);
app.use(require('connect-flash')());
app.use(bodyParser.json({limit: '100kb'})); // support json encoded bodies
app.use(bodyParser.urlencoded({
extended: true,
limit: '100kb'
})); // support encoded bodies
/**Middlewares**/
app.use(require('./middlewares/flash'));
app.use(require('./middlewares/passSession'));
/**controllers**/
require('./controllers/print.js').addRouter(app);
require('./controllers/admin.js').addRouter(app);
require('./controllers/login.js').addRouter(app);
app.use(function(err, req, res, next) {
console.error(err);
if (err.name === 'PayloadTooLargeError' ) {
req.flash('error', 'Your code is too big');
} else {
req.flash('error', 'Some error occured');
}
return res.redirect('/');
});
app.get('*', function(req, res) {
req.flash('error', 'Page not found');
return res.status(404).redirect('/');
});
if (require.main === module) {
server.listen(app.get('port'), function() {
console.log(`Server running at port ${app.get('port')}`);
});
} else {
module.exports = {
server,
app
};
}