-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (70 loc) · 1.97 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
const express = require('express');
const fileupload = require('express-fileupload')
const promisify = require('es6-promisify');
const bodyParser = require('body-parser');
const session = require('express-session');
const passport = require('passport');
const mongoose = require('mongoose');
const cloudinary = require('cloudinary').v2;
const MongoStore = require('connect-mongo')(session);
const expressValidator = require('express-validator');
const app = express();
const routes = require('./routes');
require('./handlers/passport');
// Dot Env
require('dotenv').config({
path: 'variables.env'
});
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUD_API_KEY,
api_secret: process.env.CLOUD_API_SECRET
});
// Mongoose
mongoose.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.set('useFindAndModify', false);
mongoose.Promise = global.Promise;
mongoose.connection.on('error', (err) => {
console.log('Une erreur est survenue avec la base de données : ' + err);
})
// Express session
app.use(session({
secret: process.env.SECRET,
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
cookie: { maxAge: 365 * 24 * 60 * 60 * 1000 } // preserve session for a year
}));
app.use(fileupload({
useTempFiles: true
}));
// Passport.js
app.use(passport.initialize());
app.use(passport.session());
// Setting up views
app.set('views', (__dirname + '/views'));
app.set('view engine', 'pug');
// Public folder
app.use(express.static(__dirname + '/public'));
app.use('/uploads', express.static(__dirname + '/public'));
// Body Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Express validator
app.use(expressValidator());
// Locals
app.use((req, res, next) => {
res.locals.user = req.user || null;
next();
});
// Setting up the routes
app.use('/', routes);
const port = process.env.PORT || 3000;
// Starting the server
app.listen(port);
console.log("App started");