-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (93 loc) · 3.11 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const express = require("express");
const cookieParser = require("cookie-parser");
const app = express();
const port = process.env.PORT || 8000;
const expressLayouts = require("express-ejs-layouts");
const db = require("./config/mongoose");
const flash = require("connect-flash");
const custom_flash_middleware = require("./config/custom-flash-middleware");
// for the session cookie
const session = require("express-session");
const passport = require("passport");
const passportJWT = require("./config/passport-jwt-strategy");
const passportLocal = require("./config/passport-local-strategy");
// for storing cookie session persistently in database
const MongoStore = require("connect-mongo");
// **********************
//for chats , socket io
const chatServer = require("http").Server(app); //passing express app
// pass chat sockets to chat server
const chatSocket = require("./config/chat-sockets").chatSockets(chatServer);
chatServer.listen(process.env.port2 || 3000);
console.log("chat server is listening on port 3000 ... ");
// **********************
const env = require("./config/environment");
//for scss
const sassMiddleware = require("node-sass-middleware");
const path = require("path");
if (env.name == "development") {
app.use(
sassMiddleware({
// current path , ./assets/
src: path.join(__dirname, env.asset_path, "scss"),
dest: path.join(__dirname, env.asset_path, "css"),
debug: true,
outputStyle: "extended",
prefix: "/css", // in templates we have used the "/css" as the prefix so we use it here
})
);
}
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(env.asset_path));
// to access avatars from browser
app.use("/uploads", express.static(__dirname + "/uploads"));
app.use(expressLayouts);
// extract style and scripts from sub pages into the layout
app.set("layout extractStyles", true);
app.set("layout extractScripts", true);
// set up view engine
app.set("view engine", "ejs");
app.set("views", "./views"); // without using path module we can do ./view here
//mongoStore is used for storing session cookie in database
app.use(
session({
name: "stack_infinity",
//change the secret key value before deployment
secret: env.session_cookie_key,
saveUninitialized: false,
resave: false,
cookie: {
maxAge: 1000 * 60 * 60, // session cookie expires after this time
},
store: MongoStore.create(
{
mongoUrl: env.db,
mongoConnection: db,
autoRemove: "disabled",
},
function (err) {
//callback function
console.log(err || "connect-mongodb setup is working...");
}
),
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(passport.setAuthenticatedUser);
app.use((req, res, next) => {
// console.log(req.session)
// console.log(req.user)
next();
});
app.use(flash());
app.use(custom_flash_middleware.setFlash);
// use express router
app.use("/", require("./routes"));
app.listen(port, function (err) {
if (err) {
console.log(`Error in running the server ${err}`);
}
console.log(`Server is running on port ${port}`);
});