NodeJs Decode to readable text - node.js

PROBLEM
I want to receive data from a device using IP Address via NodeJs. But I received the following data:
What I've Tried
This is the code that I've been able to get, which still produces the problem I described above.
var app = require('http').createServer(handler);
var url = require('url') ;
var statusCode = 200;
app.listen(6565);
function handler (req, res) {
var data = '';
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
console.log(data.toString());
fs = require('fs');
fs.appendFile('helloworld.txt', data.toString(), function (err) {
if (err) return console.log(err);
});
});
res.writeHead(statusCode, {'Content-Type': 'text/plain'});
res.end();
}
And below is the result I received for console.log(req.headers)
So my question is, how do I decode the data? and anyone know what type of data are they?

Use Buffers to handle octet streams.
function handler (req, res) {
let body=[];
req.on('data', function(chunk) {
body.push(chunk);
});
req.on('end', function() {
body = Buffer.concat(body).toString('utf8');
...

Related

How to make response data available outside the function

Data received from the server is written into variable cols. But it is empty in the output. I'm not sure if cols has now a proper value and is available to work with.
app.post('/get_columns', function (req, res, next) {
var cols = '';
var httpreq = http.request(options, function (response) {
response.on('data', function (chunk) {
cols += chunk;
console.log("body: " + cols);
});
});
httpreq.write(options_data);
httpreq.end();
console.log('cols: '+cols);
});
What output shows:
cols:
body: {"ResultSet Output":[{"NAME": "PART_COL1"},{"NAME": "PART_COL2"}],"StatusCode": 200,"StatusDescription": "Execution Successful"}
HTTP Calls are asynchronous call in Node.js and the response comes as multiple chunks through Events. When you are dealing
with HTTP request in Node.js, you have to deal with "data" & "end"
events on the response object given by HTTP.
Below examples shows how you can deal with the response in HTTP:
app.post('/get_columns', function (req, res, next) {
var cols = '';
var httpreq = http.request(options, function (response) {
// This event is triggered multiple times whenever a chunk is available.
response.on('data', function (chunk) {
cols += chunk;
});
// This event is triggered once when all the chunks are completed.
response.on('end', function (chunk) {
console.log("body: " + cols);
});
});
httpreq.write(options_data);
httpreq.end();
});
Another organized way to handle
var fetchCols = function (options, options_data) {
return new Promise((resolve, reject) => {
var cols = '';
var httpreq = http.request(options, function (response) {
// This event is triggered multiple times whenever a chunk is available.
response.on('data', (chunk) => {
cols += chunk;
});
// This event is triggered once when all the chunks are completed.
response.on('end', () => {
resolve(cols);
});
});
// This event is triggered when there is any error.
httpreq.on('error', (e) => {
reject(e);
});
httpreq.write(options_data);
httpreq.end();
});
};
app.post('/get_columns', function (req, res, next) {
fetchCols(options, options_data)
.then((cols) => {
console.log('body: ', cols);
})
.catch((err) => {
console.error('ERR: ', cols);
});
});

http request with compressed ( gzip , deflate ) response body - cannot parse body to json in nodejs

I tried a lot of time to make request from nodeJS server.
my service is a public one (the service out of my control)
i tried to look up solutions but I don't know what i'm doing wrong :\
i dont know if its matter but the response body contain few words in hebrew
With this article I tried to solve my problem
Full code from article is here
I'd love to get help to solve my problem :)
UPDATE:
here is my new code: (but still not working - explanation inside the code)
var express = require('express');
var app = express();
var port = 3429;
var request = require('request');
var zlib = require('zlib');
new lib that make the pipe thing worked:
var needle = require('needle');
then we have little bit changes:
var json = "";
var deflate = zlib.createInflate();
var gunzip = zlib.createGunzip();
// options definitions
var url = 'MY_SERVICE_URL';
var headersOptions = {
'Accept-Encoding': 'gzip,deflate,sdch' // encoding
, 'Content-Type': 'text/plain; charset=utf-8' // content type
, 'Accept-Language': 'en-US,en;q=0.8,he;q=0.6' // language
};
var options = {
compressed: true
, headers: headersOptions
};
app.listen(port, function () {
console.log("Listening on " + port);
setTimeout(function () {
needleTest();
}, 5000);
});
var needleTest = function () {
needle.get(url, options, function (err, response, body) {
debugger;
console.log(response.body);
response.pipe(gunzip);
response.pipe(deflate);
});
};
response.body is:
��{
"id" : "39",
"title" : "���",
"data" : []
}
after it:
response.pipe(gunzip) and response.pipe(deflate)
its goes to zlib method 'end'
the problem now is: the variable of callback function is undefined
zlib methods:
/* zlib ===> deflate */
deflate.on('data', function (data) {
debugger;
console.log('deflate data');
console.log(data);
json += data.toString();
});
deflate.on('end', function (data) {
debugger;
console.log('deflate end');
console.log(data);
parseJSON(json);
});
deflate.on('error', function (data) {
debugger;
console.log('deflate error');
console.log(data);
});
/* zlib ===> gunzip */
gunzip.on('data', function (data) {
debugger;
console.log('gunzip data');
console.log(data);
json += data.toString();
});
gunzip.on('end', function (data) {
debugger;
console.log('gunzip end');
console.log(data);
parseJSON(json);
});
gunzip.on('error', function (data) {
debugger;
console.log('gunzip error');
console.log(data);
});
function parseJSON(json) {
// do the parse things
}

How can I fetch req.param and get body in Node.js?

I found this example to get body, but how can I retrive the req.param variable? I do not have req in this case?!
app.post('/:tableName', function(res){
var data = '';
res.on('data', function (chunk){
data += chunk;
});
res.on('end',function(){
var obj = JSON.parse(data);
var schema = require('./schema/' + req.param('tableName'));
var record = new schema(obj);
record.save(function(err) {
if (err) {
console.log(err);
res.status(500).json({status: 'failure'});
} else {
res.json({status: 'success'});
}
});
})
});
UPDATE
I modified the method signature like this, but then first res.on will not get called.
app.post('/:tableName', function(req, res) {
Your callback should take in req and res, but is currently only taking in req as res.
app.post('/:tableName', function(req, res){ should give the function what it expects.

I am trying to read image from different server and write on response

function media(req,res){
console.log(req.query.image);
var collectionName = 'imageTable';
var selector = MongoHelper.idSelector(req.query.image);
MongoHelper.findOne(selector, collectionName, function(err, image) {
console.log(image.picture);
var url_parts = url.parse(image.picture);
var options = {host: url_parts.hostname, path: url_parts.pathname};
http.get(options).on('response', function (response) {
var body = '';
var i = 0;
response.on('data', function (chunk) {
i++;
body += chunk;
console.log('BODY Part: ' + i);
});
response.on('end', function () {
console.log('Finished');
res.writeHead(200,{'Content-Type':'image/JPEG'});
res.write(body);
res.end();
});
});
});
}
I am fetching image from different server. I have url of that image. And I am writing the response. But here response image is get corrupted. Any idea about how to write jpeg image in response?
function media(req,res){
console.log(req.query.image);
var collectionName = 'facebook';
var selector = MongoHelper.idSelector(req.query.image);
MongoHelper.findOne(selector, collectionName, function(err, image) {
var url_parts = url.parse(image.picture);
var options = {host: url_parts.hostname, path: url_parts.pathname};
http.get(options).on('response', function (response) {
res.writeHead(200,{'Content-Type':'image/JPEG'});
response.on('data', function (chunk) {
res.write(chunk);
});
response.on('end', function () {
res.end();
});
});
});
}
Here I got the solution. Instead of writing whole data at the end. Write it each time you get and end the response when you reach to the end of file. But still if anyone have better idea can write here.

Read remote file with node.js (http.get)

Whats the best way to read a remote file? I want to get the whole file (not chunks).
I started with the following example
var get = http.get(options).on('response', function (response) {
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
I want to parse the file as csv, however for this I need the whole file rather than chunked data.
I'd use request for this:
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
Or if you don't need to save to a file first, and you just need to read the CSV into memory, you can do the following:
var request = require('request');
request.get('http://www.whatever.com/my.csv', function (error, response, body) {
if (!error && response.statusCode == 200) {
var csv = body;
// Continue with your processing here.
}
});
etc.
You can do something like this, without using any external libraries.
const fs = require("fs");
const https = require("https");
const file = fs.createWriteStream("data.txt");
https.get("https://www.w3.org/TR/PNG/iso_8859-1.txt", response => {
var stream = response.pipe(file);
stream.on("finish", function() {
console.log("done");
});
});
http.get(options).on('response', function (response) {
var body = '';
var i = 0;
response.on('data', function (chunk) {
i++;
body += chunk;
console.log('BODY Part: ' + i);
});
response.on('end', function () {
console.log(body);
console.log('Finished');
});
});
Changes to this, which works. Any comments?
function(url,callback){
request(url).on('data',(data) => {
try{
var json = JSON.parse(data);
}
catch(error){
callback("");
}
callback(json);
})
}
You can also use this. This is to async flow. The error comes when the response is not a JSON. Also in 404 status code .

Resources