-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
66 lines (47 loc) · 1.49 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
const express = require('express');
const session = require('express-session');
const passport = require("passport");
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo');
const adminrouter = require('./routes/app.router');
require('dotenv').config();
require('./config/passport')
const routes = require('./routes');
const app = express();
mongoose.connect(process.env.DB_STRING, () => { console.log("Connected to MongoDB.") });
// Session Store for storing session data by express-sessions
const sessionStore = MongoStore.create({
mongoUrl: process.env.DB_STRING,
collection: "sessions",
});
// Compulsory Middlewares
app.use('/admin',adminrouter); //Admin route
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use('/public',express.static(__dirname + '/public')); //If static files does not load try: app.use(express.static('public'));
// Session Middleware
app.use(session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false,
store: sessionStore,
autoRemove: 'native',
cookie: {
maxAge: 1000 * 60 * 60 * 24, // 1-Day
name: 'login'
}
}))
app.use(passport.initialize());
app.use(passport.session());
// Rendering Engine Middleware (ejs)
app.set('view engine', 'ejs');
app.use(routes);
// Port for running instance
const PORT = process.env.PORT;
app.listen(PORT , (err) => {
if(err){
console.log(err);
}else{
console.log(`app is listening to port ` + PORT);
}
});