NODE JS cancel request

To add to @Etai's answer, you need to require the request module before using it for one instance of the request. Something like this:

var request = require('request');
// ...
// then later in the code
var urlrequest = request.get(uri, function(err, response, body) {
    // process data here
});

// later, you'd abort this as:
urlrequest.abort();

Note that I'm saving the instance with var urlrequest = request.get(params, callback); so that I can call abort on it later.


your currentVideoRequest is a constructor for a request object, not a request object, which is why this is failing.

The request constructor returns a request object when invoked, i.e.

require('request')('uri', function(err, resp, body){})

you can use abort() method to stop that request.

var reqObj = request({uri: 'https://jsonplaceholder.typicode.com/albums'  }, function (error, response, body) {
    console.log('API requested ') ;
    if (!err){
        console.log(body);
    }
    else
    {
        console.log(err);
    }
});

reqObj.abort();