Node.js - JSON response to request cut off - javascript

I've distilled my issue to some really basic functionality here. Basically, we're sending a request to a server (you can go ahead and c/p the URL and see the json document we get in response).
We get the response, we pipe it into a write stream and save it as a .json file - but the problem is that the file keeps being cut off. Is the .json file too large? Or am I missing something? Node.js newbie - massively appreciate any help I can get.
var fs = require('fs');
var url = 'https://crest-tq.eveonline.com/market/10000002/history/?type=https://crest-tq.eveonline.com/inventory/types/34/'
var request = require('request');
request(url).pipe(fs.createWriteStream('34_sell.json'));

var fs = require('fs');
var request = require('request');
var url = 'https://crest-tq.eveonline.com/market/10000002/history/?type=https://crest-tq.eveonline.com/inventory/types/34/';
request(url).pipe(fs.createWriteStream('34_sell.json'));
Not an answer but this is the code I used. I'm using request version 2.74.0. And node version v5.4.1.

Try writing a get request to the url and send the json as response and write an error handling statement like if err then throw err..console log and see the result..hope it works

Related

How to save big object to file nodejs?

I have a big object that I need to send from server (nodejs) to client.
But every time I try send I get "invalid string length" error.
And it's ok, because the object is really big. That's why I'd like to save it to file and then send the file to client.
I don't know the depth of the object. The object itself is and octree.
I don't have any code of saving an object to file, because every time I think about this it leads me to stringify the object, that latter leads to "invalid string length".
Here is a screenshot of the object. Every q(n) key has the same recursive structure as result key.
Thanks!
Firstly try to save in a JSON file and send JSON file directly to client-side
const fs = require('fs');
fs.writeFileSync("file_name.json", data);
res.header("Content-Type",'application/json');
res.sendFile(path.join(__dirname, 'file_name.json'));
A good solution to handling large data transfer between server and client is to stream the data.
Pipe the result to the client like so.
const fs = require('fs');
const fileStream = fs.createReadStream("path_to_your_file.json");
res.writeHead(206, {'Content-Type': 'application/json'})
fileStream.pipe(response)
or follow this blog which stringifies each element at a time and concatenates them.
I will suggest you stream it though.
You could try something like this, I'm unsure if Express would throw the same length error by using res.json.
// Assuming you're using Express and data is valid JSON.
res.json(data);

How to get number of directory items in Node.js

I need to get number of items of specific directory in Node.js
If I get the items like
var dirItems = fs.readdirSync(__dirname+'/my_dir');
and then get specific item like
dirItems[1]
everything is ok
But if I try to get their number like
dirItems.length
or
Object.keys(dirItems).length
the page doesn't work in the browser
How to get the number of directory items?
UPDATED
My full code:
var http = require('http'),
fs = require('fs');
http.createServer(function(req, res) {
var dirItems = fs.readdirSync(__dirname+'/my_dir');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(dirItems.length);
}).listen(80, 'localhost');
I was able to reproduce the error nyou get.
res.end() for the basic http server class is very picky about what you send it. You must give it a string (the error you got should have been a big clue here).
So, change this:
res.end(dirItems.length);
to this:
res.end(dirItems.length.toString());
And, it works for me. I was able to reproduce your original error and then make it work by making this simple change.
Logically, you can only send string data as an http response so apparently res.end() isn't smart enough to attempt a string conversion on its own. You have to do it yourself.
FYI, if you use a higher level framework like Express, it is more tolerant of what you send it (it will attempt a string conversion in a situation like this).
Here is how I would do it:
const fs = require('fs');
const dir = './somedir';
fs.readdir(dir, (err, files) => {
console.log(files.length);
});

Need simple Express.js XML server example

I have difficulties in something that should be very simple:
Building a simple Express.js server/listener that will receive XML file (lets's say from Postman) and parse it.
Tried different approaches, like XML2J LibXMLjs- nothing works.
Since I am new also to Postman: please advice me also how to post the message from there. (tried Body/Raw/XML - did not work for me).
You can parse xml with xml2js like so:
var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>";
parseString(xml, function (err, result) {
console.dir(result);
});

Uncaught ReferenceError: request is not defined

i am working on simple node js module ...
a module that, when i give an ID and a request rate, returns a readable stream which emits location data for the corresponding satellite...
i am trying to implement by using sub classing ReadableStream
http://nodejs.org/api/stream.html#stream_class_stream_readable
i am using this api
https://api.wheretheiss.at/v1/satellites/25544
providing my code below..
http://jsfiddle.net/omb3rwqn/1/
var request = require('request');
var url = 'https://api.wheretheiss.at/v1/satellites/25544'
var reader = request(url);
readable.on('readable', function() {
console.log('got %d characters of string data');
})
did you on accident in your app.js end-point do
app.post('/api/v1/something',function(req,res)
{
var ip = req.headers['x-forwarded-for'];
**var a = request.connection.remoteAddress**
in which case the 'request' in your code is a red herring. what you are really looking for is the fact that your end-point defines 'req' (or anything other than request)

Http message on node.js

I'm a total noob in node.js (and internet tech. in general). On my assignment from the uni. in which we were asked to develop an http server, I have been requested to do the following:
Implementation details: upon receiving a message from the ‘client’ (socket, data and end events), assume it’s an HTTP message and parse it, if it’s not(an HTTP message) you should return an HTTP response(status 400).
My question is: How to parse the the message given, and how should I expect the message to look like? Bottom line, how does an http message looks like?
Thank you!
Node itself uses a http_parser written in C.
It's based on NGINX's HTTP parser with some extensions by the node core team.
Node's http module then [uses it](var HTTPParser = process.binding('http_parser').HTTPParser;)
var HTTPParser = process.binding('http_parser').HTTPParser;
For example the ClientRequest::onSocket uses a parser.
ClientRequest.prototype.onSocket = function(socket) {
var req = this;
process.nextTick(function() {
var parser = parsers.alloc();
// [snip]
});
};
If you actually want to write your own parser then have fun parsing the HTTP protocol.
If you don't know how to write a parser, then read up on Parsing

Categories

Resources