-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (50 loc) · 1.28 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
var http = require('http'),
sys = require('sys'),
io = require('socket.io'),
url = require('url'),
fs = require('fs'),
path = require('path');
server = http.createServer(function(req, res) {
var uri = url.parse(req.url).pathname;
var filename = path.join(process.cwd(), uri);
path.exists(filename, function(exists) {
if(exists) {
fs.readFile(filename, "binary", function(err, file) {
if(err) {
send500(res, err);
} else {
send200(res, file);
}
res.end();
});
} else {
send404(res);
res.end();
}
});
});
server.listen(8080);
console.log('Server running at http://127.0.0.1:8080/');
send200 = function(res, file) {
res.writeHead(200);
res.write(file, "binary");
};
send404 = function(res) {
res.writeHead(404, {"Content-Type": "text/plain"});
res.write("404 Not Found\n");
};
send500 = function(res, err) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.write(err + "\n");
};
// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
// new client
var message = new Buffer("You have connected")
client.send(message);
client.on('message', function(data){
sys.puts(data);
})
client.on('disconnect', function(){ })
});