i'm using Node.js and this my source code...
const request = http.request(options, (response)=>{
let chunks_of_data = [];
response.on('data', (fragments) => {
chunks_of_data.push(fragments);
});
response.on('end', () => {
let response_body = Buffer.concat(chunks_of_data);
let final = response_body.toString();
console.log(final);
});
response.on('error', (error) => {
console.log(error);
});
});
request.on('error', (error) => {
console.log('Error Code: ' + error.code);
console.log('Error Message: ' + error.message);
});
console.log(request.final);
The code is working and I received the right response from the other side of the server but is it possible to make the response turn into a string variable outside the http.request, the
console.log(request.final);
in the end of my code is not working. Can anyone help me?