-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.coffee
41 lines (34 loc) · 1.05 KB
/
web.coffee
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
http = require 'http'
url = require 'url'
fs = require 'fs'
responser = (request, response) ->
query = url.parse(request.url, true).query
status = query.status || 200
serveFile = (file) ->
fs.stat file, (err, stat) ->
response.writeHead status, 'Content-Length': stat.size
stream = fs.createReadStream file
stream.pipe response
if query.respond
if match = query.respond.match /([0-9]{1,3})s/
setTimeout ->
response.writeHead status
response.end()
, parseInt(match[1]) * 1000
else if match = query.respond.match /([0-9]{1,3})m/
file = "files/" + match[0]
size = parseInt(match[1])
baseFile = "files/1m"
fs.exists file, (exists) ->
if exists
serveFile file
else
fs.readFile baseFile, (err, data) ->
data = new Array(size + 1).join(data)
fs.writeFile file, data, ->
serveFile file
return
response.writeHead status
response.end()
port = process.env.PORT || 5000
http.createServer(responser).listen port