-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
98 lines (67 loc) · 2.34 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
//require express and routes as well as mongose
const express = require('express');
const session = require('express-session');
const mongoose = require('mongoose');
const router = require('./routes/index');
const path = require('path');
const favicon = require('serve-favicon');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const sessionStore = require('connect-mongo')(session);
const promisify = require('es6-promisify');
const errorHandlers = require('./handlers/errorHandlers');
const passport = require('passport');
//require passport handler
require('./handlers/passport');
//Create the express app
const app = express();
//use Express Validator
app.use(expressValidator());
// app.use(forceDomain({
// hostname: 'www.chhotu.org'
// }));
// view engine setup
app.set('views', path.join(__dirname, 'views')); // this is the folder where we keep our pug files
app.set('view engine', 'pug'); // we use the engine pug, mustache or EJS work great too
// tell Express to serve files from our public folder
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'bower_components')));
//initialize Sessions
app.use(session({
secret: process.env.SECRET,
key: process.env.KEY,
resave: false,
saveUninitialized: false,
store: new sessionStore({ mongooseConnection: mongoose.connection })
}));
//Use our flashes
app.use(flash());
//serving favicon
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
// handles JSON bodies
app.use(bodyParser.json());
// handles URL encoded bodies
app.use(bodyParser.urlencoded({ extended: true }));
// // Passport JS is what we use to handle our logins
app.use(passport.initialize());
app.use(passport.session());
// pass variables to our templates + all requests
app.use((req, res, next) => {
res.locals.flashes = req.flash();
res.locals.user = req.user || null;
res.locals.currentPath = req.path;
next();
});
//Using our routes
// app.all(/.*/, (req, res, next)=> {
// var host = req.header("host");
// if (host.match(/^www\..*/i)) {
// next();
// } else {
// res.redirect(301, "http://www." + host);
// }
// });
app.use('/', router);
//Exporting app.js so that we can start it at start.js
module.exports = app;