-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe.js
More file actions
40 lines (32 loc) · 1.2 KB
/
e.js
File metadata and controls
40 lines (32 loc) · 1.2 KB
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
var fs = require('fs'),
ws = require('ws'),
sys = require('sys'),
url = require('url'),
http = require('http'),
path = require('path');
var httpServer = http.createServer( function(request, response) {
var pathname = url.parse(request.url).pathname;
if (pathname == "/") pathname = "index.html";
var filename = path.join(process.cwd(), 'public', pathname);
path.exists(filename, function(exists) {
if (!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found");
response.end();
return;
}
response.writeHead(200, {'Content-Type': mime.lookup(filename)});
fs.createReadStream(filename, {
'flags': 'r',
'encoding': 'binary',
'mode': 0666,
'bufferSize': 4 * 1024
}).addListener("data", function(chunk) {
response.write(chunk, 'binary');
}).addListener("close",function() {
response.end();
});
});
});
var server = ws.createServer({server: httpServer});
server.listen(8000);