-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
199 lines (171 loc) · 4.92 KB
/
app.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var express = require('express');
var app = express();
var exphbs = require('express-handlebars');
var session = require('express-session');
var mongoose = require('mongoose');
var bparse = require('body-parser');
var fs = require('fs');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('./models/user');
var siofu = require("socketio-file-upload");
var randomMC = require('random-material-color');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = 8080;
// function to encode file data to base64 encoded string
function base64_encode(file) {
// read binary data
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
return new Buffer(bitmap).toString('base64');
}
//Handlebars init
app.engine('handlebars', exphbs({
helpers: {
equal: function (a, b, options) { return (a == b) ? options.fn(this) : options.inverse(this); }
},
defaultLayout: 'main'
}));
app.set('view engine', 'handlebars');
// Body-parser init
app.use(bparse.urlencoded({ extended: false }));
app.use(bparse.json());
// Static files
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/uploads'));
// MongoDB Database init
mongoose.connect('mongodb://localhost:27017/chat-app');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('LOGGED | MongoDB Connected - ' + new Date());
});
// Message Collection
var MessageSchema = mongoose.Schema({
msg:Object
});
var Msg = mongoose.model('msg', MessageSchema);
// passport config
app.use(session({ secret: 'anything' }));
app.use(passport.initialize());
app.use(passport.session());
// File uploader init
app.use(siofu.router);
passport.use(new LocalStrategy(
function(username, password, done) {
User.getUserByUsername(username, function(err, user){
if(err) throw err;
if(!user){
return done(null, false, {message: 'Unknown User'});
}
User.comparePassword(password, user.password, function(err, isMatch){
if(err) throw err;
if(isMatch){
return done(null, user);
} else {
return done(null, false, {message: 'Invalid password'});
}
});
});
}));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.getUserById(id, function(err, user) {
done(err, user);
});
});
app.get('/', function(req, res){
res.redirect('register');
});
app.get('/register', function(req, res){
res.render('register');
});
app.post('/register',function(req,res) {
var username = req.body.username;
var password = req.body.password;
var color = randomMC.getColor();
var newUser = new User({
username: username,
password: password,
color:color
});
User.createUser(newUser, function(err, user){
if(err) throw err;
console.log(user);
});
res.redirect('/login');
});
app.get('/login', function(req, res) {
res.render('login', {user: req.user});
});
app.post('/login',
passport.authenticate('local', {successRedirect:'/chat'}),
function(req, res) {
req.session.user = req.user;
res.redirect('/chat');
});
var users = [],x=0;
io.on('connection', function(socket){
socket.on('login', function(data){
if(!users.includes(data))
users.push(data);
io.emit('online users', users);
});
socket.on('disconnect', function(){
console.log('user ' + users[socket.id] + ' disconnected');
io.emit('user disconnected',socket.id)
});
socket.on('typing', function (data) {
socket.broadcast.emit('typing', data);
});
socket.on('chat message', function(msg){
Msg.find({},function(err,document) {
var message = new Msg({
msg:msg
});
message.save();
})
io.sockets.emit('new message', msg);
});
var uploader = new siofu();
uploader.dir = __dirname + "/uploads";
uploader.listen(socket);
uploader.on("start", function(event){
console.log("Upload start");
if (/\.exe$/.test(event.file.name)) {
console.log("Aborting: " + event.file.id);
uploader.abort(event.file.id, socket);
}
});
// Do something when a file is saved:
uploader.on("saved", function(event){
var base64str = base64_encode(__dirname + "/uploads/" + event.file.name);
socket.emit('uploaded',base64str);
});
// Error handler:
uploader.on("error", function(event){
console.log("Error from uploader", event);
});
});
app.get('/chat', redirectToChatIfLoggedIn, function(req,res) {
Msg.find({},function(err,document) {
res.render('chat',{messages:document,user:req.user.username,color:req.user.color});
})
});
app.get('/logout', function(req, res){
req.session.destroy(function (err) {
res.redirect('/login');
});
});
function redirectToChatIfLoggedIn(req, res, next) {
if (!req.isAuthenticated()){
return res.render('login');
}
return next();
}
http.listen(port, function(){
console.log('listening on *:' + port);
});