Skipping request node.js - node.js

I have urlARRAY that has some urls.
I did a loop and I did request to urls in the array one by one.
Some sites in urlARRAY are loading slow. Can I skip these?
var req = require('request');
urlARRAY.forEach(function(url) {
req(url, function (error, response, html) {
if (!error && response.statusCode == 200) {
console.log("success");
} else {
console.log("error");
}
});
}

Related

How to call rest api in express js

I am new in nodejs and I am using express js, I am calling REST API and want to show the response, but my problem is that the response is showing in console.log but I want to pass the body(response) in assistant.ask, where I am wrong here is my code:
var request = require('request');
let rawInput = function (assistant) {
let rawInput = assistant.getRawInput();
request.post(
'http://xxxxxxx.ngrok.io/api/v1/240/respond',
{ json: { query: rawInput } },
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
assistant.ask(body);
}
else{
console.log(error);
}
}
);

How to make response of request module nodejs to object?

I'm using request npm module
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
})
my response return format such as "string". How to convert response to object?
I found answer
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info ) // Show the HTML for the Google homepage.
}
})

Accessing GET response body parameters

I am using the request package to make a simple HTTP GET request to the npm API. I am trying to get the download counts for npm packages from an arbitrary function in my nodeJS backend.
Here is my updateDownloadCount.ts file:
export function updateDownloads() {
plugin.find(function (err, plugins: Array<any>) {
for (let plugin of plugins) {
var url = 'https://api.npmjs.org/downloads/point/last-month/' + plugin.package;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
})
}
})
}
So that's fine and I get a string of outputs like:
{"downloads":17627637,"start":"2016-08-29","end":"2016-09-27","package":"request"}
however, when I try to access just the downloads count, i.e.
console.log(body.downloads);
I get undefined console logged... How can I access the body variables? I feel like this should be super simple, but I couldn't find any docs on it.
Try to parse body if it is string type
export function updateDownloads() {
plugin.find(function(err, plugins: Array < any > ) {
for (let plugin of plugins) {
var url = 'https://api.npmjs.org/downloads/point/last-month/' + plugin.package;
request(url, function(error, response, body) {
if (!error && response.statusCode == 200) {
if (body && typeof body == "string") {
body = JSON.parse(body);
}
console.log(body.downloads);
}
})
}
})
}

Returning a value from a function in node.js

Still learning node. Based upon the following:
https://github.com/request/request
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
})
I wish to create the above as a reusable block of code so thought I'd wrap it in a function passing the URL as a parameter such as:
var request = require('request');
var URL;
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
})
function fetchURL (URL) {
request(URL, function (error, response, body) {
if (!error && response.statusCode == 200) {
return body;
}
});
};
var a = fetchURL('http://www.google.com');
console.log(a);
This works however I am unsure of whether "return body" is needed as it also works without this line. Happy to received comments on my coding style too as it's all new to me.
The pattern in Node is to provide a callback as an argument to an asynchronous function. By convention, this callback function has error as its first argument. For example:
function fetchURL(url, callback) {
request(url, function(error, response, body) {
if (!error && response.statusCode == 200) {
callback(null, body);
} else {
callback(error);
}
});
};
fetchURL('http://www.google.com', function(err, body) {
console.log(body);
});
Note that in your snippet, return body; is a return from the anonymous callback function passed into fetchURL(). fetchURL() itself returns nothing.

nodejs async request stop working but no mistake

var request = require('request');
var async = require('async');
require('http').globalAgent.maxSockets = 30;
var count = 0;
var urlArr = ['url1','url2'.......] // ten thousand links to request
async.eachLimit(urlArr, 30, iterator, function(err) {
console.log('complete');
});
function iterator(url,callback) {
request(url, function(error, response, body) {
console.log(count++);
if (!error && response.statusCode === 200) {
return callback(null);
} else{
return callback(null);
}
});
}
The code above stop working after a few minutes, no error happen, the process is hoding and not exit, what's the problem with it?

Resources