Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:02:21+00:00 2026-06-01T04:02:21+00:00

Sorry for the vague question.. but I’m not sure quite what the problem is.

  • 0

Sorry for the vague question.. but I’m not sure quite what the problem is.
I have a node http server that I’m using to serve JSON data to a web app. It works great, but my JSON strings are starting to get large(10-12 MB), so I want to add compression with zlib.

The JSON data is in a string variable and I want to compress and then write to the response object… but the results that go back to the client seem to always have with perfect headers, and no content. Here is my deliverResponse function:

var deliverResult = function (data, response, callback, acceptEncoding){
    var payload = callback + '(' + JSON.stringify(data) + ');';

    if (acceptEncoding.match(/\bdeflate\b/)) {
        response.writeHead(200, { 'Content-Encoding': 'deflate', 'Content-Type': 'text/javascript; charset=UTF-8' });
        zlib.deflate(payload, function(err, result){
            if(!err){
                //console.log(result.toString('utf8')); // I have data on the console here
                response.write(result.toString('utf8')); // No data sent here
           }
        });
    } else if (acceptEncoding.match(/\bgzip\b/)) {
        response.writeHead(200, { 'Content-Encoding': 'gzip', 'Content-Type': 'text/javascript; charset=UTF-8' });
        zlib.gzip(payload, function(err, result){
           if(!err){
                response.write(result.toString('utf8'));
           }
        });
    } else {
        writelog('INFO', 'Returning data without compression\n');
        response.writeHead(200, { 'Content-Type': 'text/javascript; charset=UTF-8' });
        response.write(payload);
    }

    response.end();
}

The http server examples with zlib use streams and the pipe function, but I’m not sending a file as I generate the JSON data in the app from a database, so I am basing this on the convenience method examples. My troubleshooting so far I know that the response object is good, and that result.toString(‘utf8’) outputs gobeldy-gook as expected. If I don’t send an acccept-encoding header to the server, it sends plain text perfectly – so it had got to be the compression functions.

Anyone have any idea about this? I’m pretty sure it has to to with my lack of understanding about streams, pipes, buffers and the zlib object, and it’s probably just a syntax issue, so hopefully someone who understands all this can help me out 🙂

Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:02:23+00:00Added an answer on June 1, 2026 at 4:02 am

    Solved….

    Stupid problem.. the response.write is being called in a async function, so it’s doing the write.end() before the response.write and an empty response is sent… replaced the response.write with response.end in the callback and it works perfectly 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this question is too vague, but I'd rather not muddy it's point
Sorry for the vague title, but I'm not exactly sure how I can explain
Sorry that the question of this problem might be a bit vague. I'm trying
Sorry about the vague question title, but I have these typedefs here: typedef std::list<AnimationKeyframe>
Sorry for such a vague question, but I have been searching around for the
Sorry for asking such a vague question, i'm new to using C# / ASP.NET.
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Sorry about the vague title, but i didnt know how to ask the question
Sorry for the vague question, but I hope for an experienced Haskeller this is
Sorry for vague question title, i couldn't figure out something more specific. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.