-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
79 lines (61 loc) · 2.38 KB
/
server.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
// importing the required modules
const express = require("express");
const expressLayouts = require("express-ejs-layouts");
const session = require("express-session");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo");
const bodyParser = require("body-parser");
const passport = require("passport");
const methodOverride = require("method-override");
// importing and setting up the .env file
const dotenv = require("dotenv");
const path = require("path");
dotenv.config({path: "config.env"});
// using the express module as a `app` variable
const app = express();
// Setting up the Sessions for the app
app.use(session({
secret: process.env.GOOGLE_CLIENT_SECRET,
resave: false,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: process.env.DB_CONNECT_URI,
collectionName: "sessions"
}),
cookie: { maxAge: 3 * 60 * 60 * 1000 } //the cookie expires in three hour
}));
// Setting up the Passport module for login purpose
app.use(passport.initialize());
app.use(passport.session());
app.enable("trust proxy");
// Setting up express middlewares
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.use(methodOverride("_method"));
// Set the view engine to EJS
app.use(expressLayouts);
app.set('layout', 'layouts/indexLayout');
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'Client/views'));
// Setting up static files path
app.use(express.static(path.join(__dirname, 'Client/public')));
// populating and serving static files
app.use("/css", express.static(path.join(__dirname, 'Client/public/assets/css')));
app.use("/js", express.static(path.join(__dirname, 'Client/public/assets/js')));
app.use("/img", express.static(path.join(__dirname, 'Client/public/assets/img')));
// Setting up the Routers in specific orders
app.use('/', require('./Server/routes/auth'));
app.use('/', require('./Server/routes/dashboardRouter'));
app.use('/', require('./Server/routes/mainRouter'));
// listening to the server at specified port either from the .env or default(8080)
const PORT = process.env.PORT || 8080;
// connecting to the Database and starting the sever
mongoose.connect(process.env.DB_CONNECT_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(()=>{
console.log("Connected to DB...")
app.listen(PORT, () => {
console.log(`Server up and is running at http://localhost:${PORT}`);
})
});