node.js server not receive all binary data (or client not sending all binary data) -
i'm new node.js , running problem basic, i'm sure i'm not "getting" something, here go:
i've got 2 programs...
- server.js simple express/node.js server accepts post of binary data
- client.js simple request/node.js client streams contents of large file server
i've been scouring stackoverflow suggestions , think i've small example of issue:
client:
var fs = require('fs'); var request = require('request'); fs.createreadstream('test2.zip').pipe(request.post('http://localhost:3000/'));
server:
var express = require('express'); var app = express(); app.post('/', function(req, res){ var size = 0; req.on('data', function (data) { size += data.length; console.log('got chunk: ' + data.length + ' total: ' + size); }); req.on('end', function () { console.log("total size = " + size); }); req.on('error', function(e) { console.log("error error: " + e.message); }); res.send("thanks"); }); app.listen(3000); console.log('listening on port 3000');
when send small file or if post data external client advanced rest client in chrome see expect:
got chunk: 481 total: 481 total size = 481
but if send large file see bunch of data come through , stops:
got chunk: 65405 total: 65405 got chunk: 131 total: 65536 got chunk: 65396 total: 130932 got chunk: 140 total: 131072 got chunk: 65387 total: 196459 got chunk: 149 total: 196608 got chunk: 65378 total: 261986 got chunk: 158 total: 262144 got chunk: 65369 total: 327513 got chunk: 167 total: 327680 got chunk: 65360 total: 393040 got chunk: 176 total: 393216 got chunk: 65351 total: 458567 got chunk: 185 total: 458752 got chunk: 65342 total: 524094 got chunk: 194 total: 524288 got chunk: 65333 total: 589621 got chunk: 203 total: 589824 got chunk: 65324 total: 655148 got chunk: 212 total: 655360 got chunk: 15898 total: 671258
it looks data stops without 'end' getting called. it's interesting data coming in large/small chunks.
any ideas? express choking on binary data?
node.js v0.10.7, express v3.2.4, request v2.21.0
edit: have 2 mistakes.
- never calling
res.end
- calling
res.send
before gettingend
event.
here's modified server.js
snippet res.end
line being interesting move/change.
app.post('/', function(req, res){ var size = 0; req.on('data', function (data) { size += data.length; console.log('got chunk: ' + data.length + ' total: ' + size); }); req.on('end', function () { console.log("total size = " + size); res.end("thanks"); }); req.on('error', function(e) { console.log("error error: " + e.message); }); });
with fix, works expected both small , large test files.
Comments
Post a Comment