Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chai-http write after end

Tags:

node.js

chai

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();
    });
  });
});
like image 710
bubakazouba Avatar asked Jan 02 '16 03:01

bubakazouba


1 Answers

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.

like image 118
Quy Avatar answered Sep 22 '22 08:09

Quy