0

I have the following restify handler:

var assert = require('assert-plus');
var request = require('request');

function postParseVideo(req, res, next) {
  assert.string(req.body.videoSourceUrl, 'videoSourceUrl');

  var stream = request.get({uri: req.body.videoSorceUrl});
  stream.on('response', function(parseResponse) {
    fnThatTakesAReadableStream(parseResponse, function(err, data) {
      if (err) {
        console.log(err);
        next(err);
      } else {
        res.send(201, null, {Location: data.location});
        next();
      }
    });
  });
  stream.on('error', function(err) {
    console.log(err);
    next(err);
  });
};

When I run this, neither of the stream event handlers is ever called. Instead, an error bubbles up to the restify server: {"code":"InternalError","message":"options.uri is a required argument"}. I looked at the source for request v2.54.0, and it looks like that error message must be coming from line 406 of restify.js, which reads:

return self.emit('error', new Error('options.uri is a required argument'))

I have used streams successfully in a number of other languages, but I’m new to using them in JavaScript. It seems to me like request.get is throwing a synchronous error, when it should be emitting an 'error' event. Is there something I’m fundamentally misunderstanding about how streams are implemented in Node?

Jason Whittle
  • 751
  • 4
  • 23

1 Answers1

1

Your req.body.videoSourceUrl may be defined and of type string, but it's probably empty. I can reproduce your error by doing:

request.get("")
.on('response', function(){
})
.on('error', function(){
})

I'd also think about dumping assert-plus, as:

var ass = require('assert-plus');
ass.string(new String("yes"), "no");

throws

Paul Gray
  • 546
  • 1
  • 5
  • 10