Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to post large amount of json data (not file) in nodejs/express

How to post large amount of json data (not file) in nodejs/express

Problem

So I am doing an ajax(jquery) post that uploads quite a big amount of Json data. When posting a large data, the data is generally broken into chunks. So we have to listen for post data requests and construct a full buffer of the upload data. Something like this:

    req.on('data', function(chunk) {
        console.log("upload on data "+ chunk.length);
        chunks.push(chunk);
        total+= chunk.length;
    });
    req.on('error', function(e) {
            console.log('Got Error ' + e.message);
    });
    req.on('end', function() {
        var buf = new Buffer(total)
            cur = 0;
        for (var i = 0, l = chunks.length; i 

This seems to be working if I am uploading a file with form:

function display_form(req, res) {
    res.sendHeader(200, {"Content-Type": "text/html"});
    res.write(
        '
'+ ''+ ''+ '
' ); res.close(); }

But I need to upload json data (which is dynamic). I am doing this in this way:

           $.ajax({
                type: "POST",
                url: "/upload",
                data: {"data": JSON.stringify(gamePack)},
                success: cb,
            });

Then there seems to be no callback to req 'data' or 'end'. So how are uploading files different from posting data?

Problem courtesy of: Arun

Solution

This seems to have solved my problem:

$.ajax({
        type: "POST",
        dataType: 'json',
        url: "/upload",
        contentType:"application/jsonrequest",
        data: JSON.stringify(gamePack),
        success: cb,
});
Solution courtesy of: Arun

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to post large amount of json data (not file) in nodejs/express

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×