Node.js higher-order function - node.js

I am trying to send a http request when an event is entering, but the console outputs in the higher-order function are not working.
exports.handler = async (event) => {
const req = https.get('https://test.com', (res) => {
console.log('statusCode:', res);
console.log('headers:', res.headers);
let data = "";
res.on('data', (d) => {
data += d;
});
res.on('end', () => {
let url = JSON.parse(data);
console.log(url);
});
}).on('error', (e) => {
console.error(e);
});
return 0;
};
Happy for every help i can get!

I updated the code snippet you provided,
const https = require('https');
const func = async () => {
const req = https.get('https://test.com', (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
let data = "";
res.on('data', (d) => {
data += d;
});
res.on('end', () => {
console.log({data})
let url = JSON.parse(data);
console.log(url);
});
}).on('error', (e) => {
console.error(e);
});
return 0;
};
func();
console.log('statusCode:', res.statusCode);
is the correct way to retrieve statusCode from res.
let url = JSON.parse(data);
data is not json, it cannot be parsed
res is an instanceOf IncomingMessage
https://nodejs.org/dist/latest-v14.x/docs/api/all.html#http_class_http_incomingmessage
Have a look at the documentation to understand what is passed on the events emitted
Run the updated code so that you can see what is logged to stdOut

Related

Getting api using https request function

Well I had a bit problems
function getId(username){
const https = require("https")
let id = ""
let data = ``
https.get(`https://api.roblox.com/users/get-by-username?username=${username}`, (response) =>{
response.on('data', (chunk) => {
data += chunk;
})
response.on('end', () =>{
if(data){
id = JSON.parse(data).Id
}
})
})
.on('error', (error) => {
console.log(error)
})
return id
}
So my goal here to use getId("IHZAQSTORM33") and expected result, it will return user id (1684676332). but instead, it give me (""). It give me a colons
yes I'm trying to connect to roblox api.
Use promise to return the response object as shown in the code.
You can also refer to link for the comment by nkron:
Where is body in a nodejs http.get response?
function getId(username) {
const https = require('https');
let id = '';
let data = '';
const url = `https://api.roblox.com/users/get-by-username?username=${username}`;
return new Promise((resolve, reject) => {
https
.get(url, (response) => {
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
resolve(data);
});
})
.on('error', reject);
});
//return id
};
(async () => {
const responseObject = await getId('IHZAQSTORM33');
})();
For more on async and await, refer this link:
await is only valid in async function

how get data to object when call api use https.request in nodejs

i try get data to variable 'results' when call an api another but unidentified
let results;
const req = https.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (body) => {
results = JSON.parse(body).payUrl;
});
res.on('end', () => {
console.log('No more data in response.');
});
});
console.log(results);
results = unidentified
In https.request(options,(res)=>{}) res is a stream and will emit data in chunk on event 'data'. So you won't get complete data in one go. You change code something as bellow:
let results;
let url;
const req = https.request(options, (res) => {
res.setEncoding('utf8');
let body="";
res.on('data', (chunk) => {
body = body+chunk // aggregate data
})
res.on('end', () => {
// once data is completly fetched do JSON.parse();
console.log('No more data in response.')
results = JSON.parse(body);
console.log(results)
url = results.url
console.log(url);
})
});
req.on('error', (e) => {
// listen for error
console.log(e.message);
});
Also,https.request(options,(res)=>{}) is async call so console.log(results) in your code will be executed even before api calls complete.

How to return the income message from https request using only the native https module?

i'm trying to do a HTTPS request in nodejs as follows:
var makeRequest = options => {
const req = https.request(options, res => {
// console.log('statusCode:', res.statusCode);
// console.log('headers:', res.headers);
res.on('data', d => {
process.stdout.write(d);
});
});
req.on('error', e => {
console.error(e);
});
req.end();
// return results;
};
Instead of print it i would like to return this value to another function, that should looks like:
{items:[..,..,...]}
one way would be writing the function with the callback, where the function send the request ,as the response we will getting would be stream we would have store in the temporary variable, and after successfully getting the whole response, will have to pass the response to the callback function.
const https = require('https');
const StringDecoder = require('string_decoder').StringDecoder;
const options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET'
};
function getData(callbackfun){
const req = https.request(options, (res) => {
const decoder = new StringDecoder('utf-8');
let responseData = '';
res.on('data', (data) => {
responseData += decoder.write(data);
});
res.on('end',function(){
responseData += decoder.end();
callbackfun(responseData)
})
});
req.on('error', (e) => {
callbackfun(e)
});
req.end()
}
getData((result)=>{
console.log(result);
})

Write a stream to a string and return it from a function

As part of a program I am writing, I'd like to have a helper function that shortens some long URLs that are created. I found this package:
and am attempting to change the code so that it simply stores the string in a variable and returns it. I am having some issues with scope. the variable resData is never actually updated and return is always an empty string. How can I get this returned string into the global variable and return it? Thanks
var http = require("http")
module.exports = {
shorten: function(url) {
var resData = ''
http.get('[tinyurl api endpoint]' + encodeURIComponent(url), (res) => {
res.setEncoding('utf8')
res.on('data', (chunk) => {resData += chunk})
res.on('end', () => {
resData = resData.toString()
//this contains the link, and can be console.logged
})
}).on('error', (e) => {
console.log(e)
})
return resData //returns empty string
}
};
do this
var http = require("http")
module.exports = {
shorten: function(url,cb) {
var resData = ''
http.get('[tinyurl api endpoint]' + encodeURIComponent(url), (res) => {
res.setEncoding('utf8')
res.on('data', (chunk) => {resData += chunk})
res.on('end', () => {
resData = resData.toString()
//this contains the link, and can be console.logged
cb(null,resData) //<----- use callback (thanks robertklep)
})
}).on('error', (e) => {
console.log(e)
})
//--> return resData //returns empty string as node is non-blocking io, this line will be executed before http response is received
}
};
//index.js
var http = require('./path/to/that/module')
http.shorten(url,function(error,result){ console.log(result) })
Try to use with callback function
shorten: function(url,callback) {
var resData = ''
http.get('[tinyurl api endpoint]' + encodeURIComponent(url), (res) => {
res.setEncoding('utf8')
res.on('data', (chunk) => {resData += chunk})
res.on('end', () => {
resData = resData.toString()
//this contains the link, and can be console.logged
callback(null,resData); //<----here
})
}).on('error', (e) => {
console.error(e);
callback(e,null); //<----- and here
})
}

Reading content from URL with Node.js

I'm trying to read the content from a URL with Node.js but all I seem to get are a bunch of bytes. I'm obviously doing something wrong but I'm not sure what. This is the code I currently have:
var http = require('http');
var client = http.createClient(80, "google.com");
request = client.request();
request.on('response', function( res ) {
res.on('data', function( data ) {
console.log( data );
} );
} );
request.end();
Any insight would be greatly appreciated.
try using the on error event of the client to find the issue.
var http = require('http');
var options = {
host: 'google.com',
path: '/'
}
var request = http.request(options, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
console.log(data);
});
});
request.on('error', function (e) {
console.log(e.message);
});
request.end();
HTTP and HTTPS:
const getScript = (url) => {
return new Promise((resolve, reject) => {
const http = require('http'),
https = require('https');
let client = http;
if (url.toString().indexOf("https") === 0) {
client = https;
}
client.get(url, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
resolve(data);
});
}).on("error", (err) => {
reject(err);
});
});
};
(async (url) => {
console.log(await getScript(url));
})('https://sidanmor.com/');
the data object is a buffer of bytes. Simply call .toString() to get human-readable code:
console.log( data.toString() );
reference: Node.js buffers
A slightly modified version of #sidanmor 's code. The main point is, not every webpage is purely ASCII, user should be able to handle the decoding manually (even encode into base64)
function httpGet(url) {
return new Promise((resolve, reject) => {
const http = require('http'),
https = require('https');
let client = http;
if (url.toString().indexOf("https") === 0) {
client = https;
}
client.get(url, (resp) => {
let chunks = [];
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
chunks.push(chunk);
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
resolve(Buffer.concat(chunks));
});
}).on("error", (err) => {
reject(err);
});
});
}
(async(url) => {
var buf = await httpGet(url);
console.log(buf.toString('utf-8'));
})('https://httpbin.org/headers');

Resources