I have a server-app
that receives an audio stream from the client. I am trying to test the app using chai
/chai-http
, but it gives me this error:
[Error: write after end]
What's the problem?
Code:
var chai = require('chai');
var chaiHttp = require('chai-http');
var server = require('../server-app');
var should = chai.should();
var fs = require('fs');
chai.use(chaiHttp);
describe('server', function() {
it('should work..', function (done){
var req = chai.request(server).post('/speech');
fs.createReadStream('./test.wav').pipe(req);
req.end(function (err,res){
console.log(err);//outputs: [Error: write after end]
done();
});
});
});
You're calling req.end
before the stream finishes pushing data to the request.
I would do the following:
var readstream = fs.createReadStream('./test.wav');
readstream.pipe(req);
readstream.on('close', someFunction);
readstream.on('end', someFunction);
and call req.end
in the close or end events for the stream. You're calling req.end before either of those events are emitted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With