-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (92 loc) · 2.9 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
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const MongoClient = require('mongoDB').MongoClient
const app = express();
var http = require('http');
const server = http.createServer(app);
const io = require('socket.io').listen(server);
var usernames = [];
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/css', express.static('css'));
app.use('/img', express.static('img'));
var db;
MongoClient.connect('mongodb://test-user:user123@ds111963.mlab.com:11963/test-db', function (err, database) {
if (err) return console.log(err);
db = database;
server.listen(3000, function () {
console.log('listening to port 3000');
});
});
io.sockets.on('connection', function (socket) {
console.log('Socket Connected...');
socket.on('new user', function (data, callback) {
if (usernames.indexOf(data) != -1) {
callback(false);
} else {
callback(true);
socket.username = data;
usernames.push(socket.username);
updateUsernames();
}
});
function updateUsernames() {
io.sockets.emit('usernames', usernames);
}
socket.on('send message', function (data) {
io.sockets.emit('new message', {
msg: data,
user: socket.username
});
});
socket.on('disconnect', function (data) {
if (!socket.username) {
return;
}
usernames.splice(usernames.indexOf(socket.username), 1);
updateUsernames;
});
});
app.get('/', function (req, res) {
res.sendFile('public/index.html', {
root: __dirname
});
});
app.post('/suggest', function (req, res) {
db.collection('suggest').save(req.body, function (err, result) {
if (err) return console.log(err);
console.log('saved to our db');
res.redirect('/');
})
})
app.post('/suggest2', function (req, res) {
db.collection('suggest2').save(req.body, function (err, result) {
if (err) return console.log(err);
console.log('saved to our db');
res.redirect('/about.html');
})
})
app.post('/suggest3', function (req, res) {
db.collection('suggest3').save(req.body, function (err, result) {
if (err) return console.log(err);
console.log('saved to our db');
res.redirect('/contact.html');
})
})
app.post('/suggest4', function (req, res) {
db.collection('suggest4').save(req.body, function (err, result) {
if (err) return console.log(err);
console.log('saved to our db');
res.redirect('/chat.html');
})
})
app.post('/offer', function (req, res) {
db.collection('offer').save(req.body, function (err, result) {
if (err) return console.log(err);
console.log('saved to our db');
res.redirect('/contact.html');
})
})