-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (65 loc) · 2.02 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
67
68
69
70
71
72
73
74
// Note that order matters
const express = require('express');
const app = express();
const port = 8000;
const cookieParser = require('cookie-parser');
const expressLayouts = require('express-ejs-layouts')
const db = require('./config/mongoose');
// used for session cookie
const session = require('express-session');
const passport = require('passport');
const passportLocal = require('./config/passport-local-strategy');
const MongoStore = require('connect-mongo');
const { default: mongoose } = require('mongoose');
app.use(express.urlencoded());
app.use(cookieParser());
// use static files
app.use(express.static("./assets"));
// use express layouts
app.use(expressLayouts);
// extract styles and scripts from sub pages and the layout
app.set('layout extractStyles', true);
app.set('layout extractScripts', true);
// setup view engine
app.set('view engine', 'ejs');
app.set('views', './views');
// session cookie ecryption
app.use(session({
name: 'codeial',
// change the secret key before deployment
secret: 'somethingblah',
saveUninitialized: false,
resave: false,
cookie: {
maxAge: (1000*60*100)
},
// mongo store is used to store the session cookie in db
store: MongoStore.create(
{
mongoUrl: 'mongodb://localhost/codeial_development',
// TODO not working with this
// mongooseConnection: db,
autoRemove: 'disabled'
},
function(err){
if(err){
console.log('Error initializing MongoStore client:', err);
}else{
console.log('MongoStore client initialized successfully');
}
}
)
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(passport.setAuthenticatedUser);
// use express router
app.use('/', require('./routes/index'));
app.listen(port, function(err){
if(err){
console.log(`Error in running the server: ${err}`);
return;
}
console.log(__dirname);
console.log(`Server is running at port: ${port}`);
});