-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (95 loc) · 3.06 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
106
107
108
109
110
111
112
113
114
115
116
const express = require('express');
const env = require('./config/environment');
const cookieParser = require('cookie-parser');
const app = express();
const expressLayouts = require('express-ejs-layouts');
const session = require('express-session');
const passport = require('passport');
const passportLocal = require('./config/passport-local-strategy');
const passportJWT = require('./config/possport-jwt-strategy');
const passportGoogle = require('./config/passport-google-oauth2-strategy');
const MongoStore = require('connect-mongo');
const sassMiddleware = require('node-sass-middleware');
const flash = require('connect-flash');
const customMware = require('./config/middleware');
const path = require('path');
const { default: mongoose } = require('mongoose');
app.use(
sassMiddleware({
src: path.join(__dirname, env.asset_path, 'scss'),
dest: path.join(__dirname, env.asset_path, 'css'),
debug: true,
outputStyle: 'expanded',
prefix: '/css',
})
);
app.use(express.urlencoded());
app.use(cookieParser());
//use static file
app.use(express.static(env.asset_path));
//make the uploads path available to the browser
app.use('/uploads', express.static(__dirname + '/uploads'));
//use express layout
app.use(expressLayouts);
//set default layout
app.set('layout', './layouts/layout');
//extract styles and scripts from subpages to layout
app.set('layout extractStyles', true);
app.set('layout extractScripts', true);
//set up view engine
app.set('view engine', 'ejs');
app.set('views', './views');
//mongo store is used to strore the session cookie in the db
app.use(
session({
name: 'kinApp',
//TODO change the secret before deployment in production mode
secret: env.session_cookie_key,
saveUninitialized: false, //don;t create session ultil something strord
resave: false, //don't save session if unmodified
cookie: {
maxAge: 1000 * 60 * 100,
},
store: MongoStore.create(
{
mongoUrl: env.db_url,
autoRemove: 'disabled',
},
function (err) {
console.log(err || 'connet-mongodb setup ok');
}
),
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(passport.setAuthenticatedUser);
//put after session is used, it uses session cookies
//it setup in cookies which stores the session info
app.use(flash());
app.use(customMware.setFlash);
//use express router after passport
app.use('/', require('./routes'));
const port = process.env.PORT || 8000;
const connectionParams = {
useNewUrlParser: true,
useUnifiedTopology: true,
};
mongoose.set('strictQuery', true);
mongoose
.connect(env.db_url, connectionParams)
.then(() => {
console.log('Connected to the database ');
const httpserver = app.listen(port, function (err) {
if (err) {
console.log(`Error in running the server :${err}`);
}
console.log(`Server is running on port: ${port}`);
});
const chatSockets = require('./config/chat_sockets').chatSockets(
httpserver
);
})
.catch((err) => {
console.error(`Error connecting to the database. ${err}`);
});