We use request to make http requests with node.js. We would like to give the user an opportunity to abort the request when he decides. That means we have to trigger the abort() from outside the request function. Maybe we can check an outside variable from inside the function. (We already tried to set the timeout to zero after the request started, that doesn't work.) Maybe we set the variable request to null. Do you know a better way to do this?
here is example code to show what kind of request we are talking about:
app.js:
var http = require('http');
var request = require("request");
http.createServer().listen(1337, "127.0.0.1");
request({uri: 'http://stackoverflow.com' }, function (error, response, body) {
console.log('url requested ') ;
if (!error){
console.log(body);
}
else
{
console.log(error);
}
});
The request function returns a request object. Just call abort() on that?
var r = request({uri: 'http://stackoverflow.com' }, function (error, response, body) {
console.log('url requested ') ;
if (!error){
console.log(body);
}
else
{
console.log(error);
}
});
r.abort();