mirror of
https://github.com/sstent/node.git
synced 2026-01-25 22:52:07 +00:00
45 lines
1008 B
JavaScript
45 lines
1008 B
JavaScript
var fs = require('fs');
|
|
var db_helper = require("./db_helper.js");
|
|
|
|
var http = require('http').createServer(function handler(req, res) {
|
|
fs.readFile(__dirname + '/index.html', function(err, data) {
|
|
if (err) {
|
|
res.writeHead(500);
|
|
return res.end('Error loading index.html');
|
|
} else {
|
|
res.writeHead(200);
|
|
res.end(data);
|
|
}
|
|
});
|
|
}).listen(3000);
|
|
|
|
var io = require('socket.io').listen(http);
|
|
io.sockets.on('connection', function(client) {
|
|
console.log('Client connected');
|
|
|
|
// populate employees on client
|
|
db_helper.get_employees(function(employees) {
|
|
client.emit('populate', employees);
|
|
});
|
|
|
|
// client add new employee
|
|
client.on('add employee', function(data) {
|
|
// create employee, when its done repopulate employees on client
|
|
db_helper.add_employee(data, function(lastId) {
|
|
// repopulate employees on client
|
|
db_helper.get_employees(function(employees) {
|
|
client.emit('populate', employees);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|