-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (87 loc) · 3.35 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
// adding all the libraries
const express = require('express');
const env = require('./config/environment');
const logger = require('morgan');
const app = express();
require('./config/view-helpers')(app);
const port= 2500;
const expressLayouts = require('express-ejs-layouts');
const db = require('./config/mongoose');
const path = require('path');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require('passport');
const passportLocal = require('./config/passport-local-strategy');
const passportJWT = require('./config/passport-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');
//setup the chat server to be used with socket.io
const chatServer = require('http').Server(app);
const chatSockets = require('./config/chat_sockets').chatSockets(chatServer);
chatServer.listen(5000);
console.log('Chat server is listening on port: 5000');
//show logs if only in development environment
if(env.name == 'development'){
// sass middleware for css
app.use(sassMiddleware({
src: path.join(__dirname, env.asset_path, 'scss'),
dest: path.join(__dirname, env.asset_path, 'css'),
debug: true,
outputStyle: 'extended',
prefix: '/css'
}));
}
// middleware for decoding the recieved data
app.use(express.urlencoded({extended: false}));
// middleware for decoding the cookie
app.use(cookieParser());
// adding the static files for styling and scripting the pages
app.use(express.static(path.join(__dirname, env.asset_path)));
//makes the file upload path available to the browser
app.use('/upload',express.static(__dirname+'/upload'));
app.use(logger(env.morgan.mode, env.morgan.options));
// adding layouts in ejs
app.use(expressLayouts);
// extract style and scripts from subpages into the layout
app.set('layout extractStyles', true);
app.set('layout extractScripts', true);
//setting up view engine
app.set('view engine', 'ejs');
app.set('views', './views');
//middleware for initiating session(stored in cookies) when authenticated
//Mongostore is used to store the session in the db
app.use(session(
{
name: 'SociPoP',
// TODO change the secret before deployment in production mode
secret: env.session_cookie_key,
saveUninitialized: false,
resave: false,
cookie: {
maxAge: (1000*60*100) //session duration in milliseconds
},
store: MongoStore.create({
mongoUrl: 'mongodb://localhost/socipop_development'
})
}
));
//middleware for authenticating user using passport js
app.use(passport.initialize());
app.use(passport.session());
//middleware to check each response whether in session
app.use(passport.setAuthenticatedUser);
//custom middleware to attach flash messages
app.use(flash());
app.use(customMware.setFlash);
//use express router
app.use('/',require('./routes'));
//firing up the server
app.listen(port, function(err){
if(err){
console.log(`Error in running the server : ${err}]`);
}
console.log(`Server is running on port: ${port}`);
});