-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
126 lines (101 loc) · 2.96 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
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
117
118
119
120
121
122
123
124
125
126
/**
* Loading all dependencies
*/
var express = require('express');
var app = express();
var bodyP = require('body-parser');
var path = require('path');
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var mongojs = require('mongojs');
var favicon = require('serve-favicon');
var ObjectId = mongojs.ObjectId;
var mongoose = require('mongoose');
var passport = require('passport');
var expressValidator = require('express-validator');
var flash = require('connect-flash');
var configDB = require('./config/database.js');
var morgan = require('morgan');
var session = require('express-session');
var mailer = require('express-mailer');
var authConfig = require('./config/auth');
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyP.json());
app.use(bodyP.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
//db connections
var db = mongojs('sockchat', ['messages']);
mongoose.connect(configDB.url, {
useMongoClient: true,
});
app.use(session({
secret: 'ilovescotchscotchyscotchscotch',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash());
require('./config/passport')(passport, app);
var port = process.env.PORT || 3000;
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
// app.use(session({ secret: 'tameeshb' })); // session secret
// Express Validator
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
mailer.extend(app, {
from: authConfig.emailCreds.email,
host: 'smtp.gmail.com',
secureConnection: true,
port: 465,
transportMethod: 'SMTP',
auth: {
user: authConfig.emailCreds.email,
pass: authConfig.emailCreds.pass
}
});
//routing
require('./routes/index.js')(app, passport, db);
require('./routes/users.js')(app, passport, db);
// persistent login sessions
// use connect-flash for flash messages stored in session
// app.listen(port);
//404
app.use(function (req, res, next) {
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.render('404', { url: req.url });
return;
}
// respond with json
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
server.listen(process.env.PORT || 3000);
console.log('The magic happens on port ' + port);
//socket programming using socket.io
require('./server_sockets.js')(app, io, db);