-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
56 lines (44 loc) · 1.41 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
const express = require('express');
const app = express();
const promisify = require('es6-promisify');
const bodyParser = require('body-parser');
const session = require('express-session');
const passport = require('passport');
const mongoose = require('mongoose');
const expressValidator = require('express-validator');
const routes = require('./routes');
require('./handlers/passport');
// Dot Env
require('dotenv').config({ path: 'variables.env' });
// Mongoose
mongoose.connect(process.env.DATABASE);
mongoose.Promise = global.Promise;
mongoose.connection.on('error', (err) => {
console.log('We have an error with the database: ' + err);
})
// Express session
app.use(session({ secret: process.env.SECRET }));
// 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('We have a server running on PORT: ' + PORT));