-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (36 loc) · 1.02 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
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var mysql = require('mysql');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "broadcast"
});
con.connect(function (err) {
if (err) throw err;
console.log("DB Connected!");
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/serverpush', function (req, res) {
con.query("SELECT * FROM candidates", function (err, result, fields) {
if (err) throw err;
console.log(result[0].name);
io.emit('client get', result);
res.sendStatus(200);
});
});
io.on('connection', function (socket) {
console.log('a user connected');
socket.on('disconnect', function () {
console.log('user disconnected');
});
});
http.listen(3000, function () {
console.log('listening on *:3000');
});