-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.js
66 lines (56 loc) · 1.69 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
65
66
import path from 'path';
import express from 'express';
import logger from 'morgan';
import config from './server/config';
const app = express();
import routes from './server/routes/index.route';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import passport from 'passport';
import session from 'express-session';
import flash from 'connect-flash';
import passportStrategy from './server/helpers/passport.strategy';
import UserAuthRoute from './server/routes/user.auth.route';
// Console the logger
if (config.env === 'development') {
app.use(logger('dev'));
}
// Middlewares
app.use(express.static(__dirname + '/dist'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Connect mongodb
mongoose.Promise = global.Promise;
mongoose.connect(config.mongo_url)
.then((result) => {
console.log("MongoDB Connected");
})
.catch((err) => {
console.log("MongoDB connection error", err);
});
// Set up passport strategy
passportStrategy(passport);
// initialize passport and passport session
app.use(session({
secret: 'passport_secret_key',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
UserAuthRoute(app, passport); // Set up the passport authentication
// Route api
app.use('/api', routes);
// Default - Route WildCard
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname,'dist','index.html'));
});
// Start Server
app.listen(config.port, 'localhost', function(err) {
if (err) {
console.log(err);
return;
}
console.log(`Listening on ${config.base_url}`);
});