-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
112 lines (92 loc) · 2.93 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
107
108
109
110
111
112
// DEPENDENCIES
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const auth = require('./middleware/auth');
// MIDDLEWARE VALIDATOR
const expressValidator = require('express-validator');
// DATABASE
const mongoose = require('mongoose');
// AUTHENTICATION
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
// ROUTES
const root = require('./routes/root');
const users = require('./routes/users');
const clients = require('./routes/clients');
const admin = require('./routes/admin');
const documents = require('./routes/documents');
// MODELS
const User = require('./model/user');
// CONFIG
const config = require('./config/env.json')[process.env.NODE_ENV || 'development'];
const app = express();
const router = express.Router();
// VALIDATOR
app.use(expressValidator());
// VIEW ENGINE
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('node-sass-middleware')({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
indentedSyntax: true,
sourceMap: true
}));
app.use(require('express-session')({
secret: 'M4LD1T0-G0RD0-C0MUN1ST4',
resave: false,
saveUninitialized: true
}));
// PASSPORT
app.use(passport.initialize());
app.use(passport.session());
// PUBLIC DIRECTORY
app.use(express.static(path.join(__dirname, 'public')));
// FLASH MESSAGES
app.use(function(req, res, next){
res.locals.success_msg = ''; // SUCCESSFULLY MESSAGES
res.locals.error_msg = ''; // SINGLE ERROR
res.locals.errors = ''; // MULTIPLE ERRORS
res.locals.user = (req.user)? req.session.passport.user : '';
next();
})
// ROUTES
app.use('/', root);
app.use('/users', users);
app.use('/clients', clients);
app.use('/admin', admin);
app.use('/documents', documents);
// PASSPORT LOCAL STRATEGY
passport.use(new LocalStrategy(auth.authenticate));
passport.serializeUser(auth.serializeUser);
passport.deserializeUser(auth.deserializeUser);
// CATCH 404 ERROR HANDLER
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
console.log(
`\n-> Launching environment on:
${config.server}
##########################################
******** LAUNCHING ${config.titles.app} ********
##########################################
Connecting database on:
${config.mongo_uri}${config.database}
##########################################\n`
);
// LAUNCH CONNECTION WITH DATABASE
mongoose.connect(config.mongo_uri + config.database);
// LAUNCH THE APP ON THE LISTENING PORT
app.listen(config.port);
module.exports = app;