Created
March 5, 2014 02:17
-
-
Save changtimwu/9359979 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http = require("http") | |
url = require("url") | |
path = require("path") | |
fs = require("fs") | |
dirname = '.' | |
servfunc = (request, response) -> | |
uri = url.parse(request.url).pathname | |
filename = path.join( dirname, uri) | |
fs.exists filename, (exists) -> | |
if not exists | |
response.writeHead 404, "Content-Type": "text/plain" | |
response.write "404 Not Found\n" | |
response.end() | |
return | |
response.writeHead 200, "Content-Type":'application/octet-stream' | |
fileStream = fs.createReadStream filename | |
fileStream.pipe response | |
serv=null | |
csockets =[] | |
@start = (portno,dn=process.cwd())-> | |
console.log "Static file server running at\n => http://localhost:#{portno}/" | |
dirname =dn | |
csockets=[] | |
serv = http.createServer(servfunc).listen portno | |
serv.on 'connection', (sk)-> | |
sk.iaddress = sk.address().address | |
csockets.push sk | |
sk.setTimeout 4000 | |
sk.on 'error', (err)-> | |
console.log 'got error = ', err | |
@status = -> | |
pending: ( | |
for sk in csockets | |
ip: sk.iaddress | |
progress: sk._bytesDispatched - 161 | |
done: sk.destroyed | |
) | |
@stop = -> | |
serv.close -> | |
console.log 'Server closed!' | |
for sk in csockets | |
sk.destroy() | |
csockets =[] | |
if require.main is module | |
portno =3030 | |
fserv = exports | |
fserv.start portno, '.' | |
setTimeout (-> | |
console.log fserv.status() | |
),3000 | |
setTimeout (-> | |
console.log fserv.status() | |
fserv.stop() | |
),10000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment