I probably have some issues with the asyncness of Node.js.
rest.js
var Shred = require("shred");
var shred = new Shred();
module.exports = {
Request: function (ressource,datacont) {
var req = shred.get({
url: 'ip'+ressource,
headers: {
Accept: 'application/json',
},
on: {
// You can use response codes as events
200: function(response) {
// Shred will automatically JSON-decode response bodies that have a
// JSON Content-Type
if (datacont === undefined){
return response.content.data;
//console.log(response.content.data);
}
else return response.content.data[datacont];
},
// Any other response means something's wrong
response: function(response) {
return "Oh no!";
}
}
});
}
}
other.js
var rest = require('./rest.js');
console.log(rest.Request('/system'));
The problem ist if I call the request from the other.js I always get 'undefined'. If I uncomment the console.log in rest.js then the right response of the http request is written to the console. I think the problem is that the value is returned before the actual response of the request is there. Does anyone know how to fix that?
Best,
dom
First off, it is useful to strip down the code you have.
Request: function (ressource, datacont) {
var req = shred.get({
// ...
on: {
// ...
}
});
}
Your Request function never returns anything at all, so when you call it and console.log the result, it will always print undefined. Your request handlers for the various status codes call return, but those returns are inside of the individual handler functions, not inside Request.
You are correct about the asynchronous nature of Node though. It is impossible for you to return the result of the request, because the request will still be in progress when your function returns. Basically when you run Request, you are starting the request, but it can finish at any time in the future. The way this is handled in JavaScript is with callback functions.
Request: function (ressource, datacont, callback) {
var req = shred.get({
// ...
on: {
200: function(response){
callback(null, response);
},
response: function(response){
callback(response, null);
}
}
});
}
// Called like this:
var rest = require('./rest.js');
rest.Request('/system', undefined, function(err, data){
console.log(err, data);
})
You pass a third argument to Request which is a function to call when the request has finished. The standard Node format for callbacks that can fail is function(err, data){ so in this case on success you pass null because there is no error, and you pass the response as the data. If there is any status code, then you can consider it an error or whatever you want.
Related
I am trying to store the response of an http request made using nodejs by request module but the problem is I can't acsess it after the request is completed in more details we can say after the callback
How I can add it
Here is what I tried till now
Tried to use var instead of let
Tried passing it to a function so that i can use it later but no luck
Here is my code can anyone help actually new to nodejs that's why maybe a noob question
var request = require('request')
var response
function sort(body) {
for (var i = 0; i < body.length; i++) {
body[i] = body[i].replace("\r", "");
}
response = body
return response
}
request.get(
"https://api.proxyscrape.com/?request=getproxies&proxytype=http&timeout=10000&country=all&ssl=all&anonymity=all",
(err, res, body) => {
if (err) {
return console.log(err);
}
body = body.split("\n");
sort(body);
}
);
console.log(response)
In this I am fetching up the proxies from this api and trying to store them in a variable called as response
var request = require("request");
var response;
async function sort(body) {
await body.split("\n");
response = await body;
console.log(response); // this console log show you after function process is done.
return response;
}
request.get(
"https://api.proxyscrape.com/?request=getproxies&proxytype=http&timeout=10000&country=all&ssl=all&anonymity=all",
(err, res, body) => {
if (err) {
return console.log(err);
}
sort(body);
}
);
// console.log(response); //This console log runs before the function still on process, so that's why it gives you undefined.
Try this code it works fine I just tested.
put the console log inside the function so you can see the result.
The console.log that you put actually runs before you process the data so that's why you are getting "undefined".
Actually, you will get the data after the sort Function is done processing.
I want to mock the following piece of code using sinon,
request(options, function(error, response) {
if (error) {
reject(error);
} else {
resolve(response);
}
});
can you help me with this ?
Assuming it as a get request,
let request = require('request') // request module.
let options = "some url" // mock data for url
requestStub = sinon.stub(request, 'get').callsArgsWith(0, options);// first argument of request call is options.
requestStub.yields(error, response)//It returns error and response. These will be mock data
sinon.assert.calledOnce(requestStub)//Test to check whether stub is called or not.
requestStub.restore(); // need to restore stub every time after use.
So depending upon what error and response you pass, it will return output accordingly.
I have the following function which performs a GET call. I want to wait for the response and then perform the next step. My code looks like this
getListOfChannels : function(token, callback){
var Channels = [];
var options = { method: 'GET',
url: url,
headers:
{
'x-api-key': token } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
var json = JSON.parse(body)
var data = json.MemberEntitlement;
for(var i=0 ; i < data.length ; i++){
if(data[i].Entitled == false){
Channels.push(data[i].ChannelNumber);
}
}
});
callback(Channels[0]);
}
Also my callback function is just printing the value
simplePrint : function(arg){
console.log(arg)
}
But still, the callback function does not wait for the full response and prints out undefined.
What should i do. I can add implicit wait but that doesnt seem like a good practice.
I think you are invoking callback after the request function invocation. It's not in request's scope, but in getListOfChannels scope. Therefore, callback is running synchronously. Try replacing:
});
callback(Channels[0]);
}
with
callback(Channels[0]);
});
}
Calling the Riot-Api Im receiving incomplete JSON on a https GET-request.
After debugging, I realized that depending how much I wait (breakpoint) pre-executing the
https on'data' callback Im actually receiving the complete JSON object.
(Average API response time for me is 200-300ms)
let getOptions = function(url) {
return {
host: 'na.api.pvp.net',
port: 443,
path: `${url}?api_key=${apiKey}`,
method: 'GET'
};
}
exports.Call = function(url, callback) {
let response = {};
let req = https.request(getOptions(url), function(res) {
response.statusCode = res.statusCode;
res.on('data', function(data) {
response.json = JSON.parse(data);
callback(response);
});
});
req.on('error', function(err) {
response.err = err;
callback(response);
});
req.end();
};
Running the code without breakpoints or only breaking a short time I run either into error:
JSON.parse(data): Unexpected Token in JSON at position ...
or
JSON.parse(data): Unexptected end of JSON Input.
As I expect the 'data' callback to be executed only after the request is complete im confused about how to fix it (without artificially delaying it ofc.).
http.request returns a stream – its not a simple callback that contains the whole response.
You will have to buffer and concatenate everything if you want to parse the whole response.
I would strongly recomment to use a helper library like got or request
I started playing with nodejs yesterday afternoon. This morning I started writing a proxy server and have the following requirements once a request has been received:
Perform auth check (through request())
If auth check evaluates to true, do actual request (proxy)
Else, redirect
All works correctly bar the actual proxy request (assigned to proxy var). It either is not being called, or at least, the response is not being pipped back to the request. Or something else, which I feel could be to do with the asynchronous behavior of nodejs.
Additional note: "Win!" is output on the console.
Any thoughts are welcomed.
var server = httpProxy.createServer(function(request, response, proxy) {
var requestHostname = request.headers['x-forwarded-host'];
var configFile = './config/'+requestHostname+'.js';
if(path.existsSync(configFile))
{
var config = require(configFile);
var authProxy = requester({
url: config.proxyRequest.url+config.proxyRequest.defaultPath,
port: 443,
method: request.method
}, function(error, proxyResp, body) {
if(config.methods.authCheck(body))
{
console.log('Win!');
proxy = requester({
url: 'http://www.google.com',
port: 443,
method: request.method
});
// Pipe request and response back
request.pipe(proxy);
proxy.pipe(response);
}
else
{
response.writeHead(300, 'Forbidden', {
'Location': globalConf.portalUrl
});
response.end();
}
});
}
else
{
response.writeHeader(400);
response.write('404: The requested URL '+requestHostname+' does not exist.');
response.end();
}
response.addListener('end', function() {
console.log('Ending it');
})
});
The problem is that you are assigning a return value to authProxy and proxy. You should call requester without assigning a value, just:
requester( object, callback );
I'm not sure what requester returns in your code, but when calling async functions, you don't typically expect a return value, handle everything else in the callback passed as parameter. Maybe if you add a sample of that code (requester function), things will be clearer :)