Created
December 4, 2011 23:16
-
-
Save aviflax/1431609 to your computer and use it in GitHub Desktop.
Trying to figure out how to determine http transfer progress accurately in Node,js
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
curl -i -# -o /dev/null http://localhost:3000/ |
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
fs = require 'fs' | |
path = require 'path' | |
http = require 'http' | |
httpServer = http.createServer() | |
filePath = "/Users/avi/Applications/Games/StarCraft II/Campaigns/Liberty.SC2Campaign/enUS.SC2Assets" | |
httpServer.on 'request', (req, res) -> | |
fs.stat filePath, (err, stats) -> | |
if err | |
res.writeHead 500, {'Content-Type': 'text/plain'} | |
res.end err.message | |
return | |
bytesSent = 0 | |
res.writeHead 200, {'Content-Length': stats.size} | |
fileStream = fs.createReadStream filePath | |
fileStream.on 'end', -> res.end() | |
fileStream.on 'data', (data) -> | |
res.write data | |
bytesSent += data.length | |
progress = (bytesSent / stats.size * 100).toFixed(2) | |
process.stdout.write "Progress: #{progress} \r" | |
httpServer.listen 3000 | |
console.log 'Listening on port 3000' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment