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

  • Home
  • SEARCH
  • 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 5971549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:31:53+00:00 2026-05-22T20:31:53+00:00

Given code like this from a node.js neophyte like me: require(‘http’).createServer(function( req, resp )

  • 0

Given code like this from a node.js neophyte like me:

require('http').createServer(function( req, resp ) {
    var pathname = require('url').parse( req.url ).pathname;

    if( req.method === "POST" ) {
        var postData = "";
        req.addListener( "data", function( postDataChunk ) {
            postData += postDataChunk;
        });
        req.addListener( "end", function() {
            // do my thing with postData
            // and end the response
        });
    }
}).listen(8888);

…every time there’s a POST request, the set of anonymous functions sent to the listeners are recreated.

This seems (almost) required since node doesn’t send a second accumulator argument to the "data" listener, or a final result argument to the "end" listener. As such, the anonymous functions need to reference the postData variable in order to build the result.

My preference is to have named functions for the listeners created outside the server request handler, like this:

function data_listener( postDataChunk ) {
    this.some_unique_property.accumulator += postDataChunk;
}
function end_listener() {
    // do my thing with postData
    // and end the response
}

require('http').createServer(function( req, resp ) {
    var pathname = require('url').parse( req.url ).pathname;

    if( req.method === "POST" ) {
        req.some_unique_property = {
            response: resp,
            pathname: pathname,
            accumulator: "",
        };
        req.addListener( "data", data_listener );
        req.addListener( "end", end_listener );
    }
}).listen(8888);

Since this in the listener functions is the request object, I’ve borrowed the request object to accumulate the post data so that I can create my functions once and reuse them.

Is this an acceptable/safe approach?

  • 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-05-22T20:31:54+00:00Added an answer on May 22, 2026 at 8:31 pm

    I prefer the latter approach versus many anonymous functions, because it makes it easier to organize code. Should the code to handle data grow large, the former has the potential to be a giant function with anonymous functions inside, and much harder to maintain.

    With the latter, I can easily break code out into separate modules to keep things organized. It also lets me document the individual handlers easily if I use something like JSDoc for documentation.

    The only recommendation I can give with the latter is some comment indicating what this is meant to represent so someone else that reads the code over doesn’t have to spend too much time thinking on it.

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

Sidebar

Related Questions

Given some JS code like that one here: for (var i = 0; i
Given the code: new Thread(new BackgroundWorker()).start(); Intuitively it feels like the BackgroundWorker instance should
Given the code from the Complex Form part III how would you go about
I was given this code a while back. I finally got around to testing
There's something very unsatisfactory about this code: /* Given a command string in which
Python 2.5.1 http://www.cgsecurity.org/wiki/After_Using_PhotoRec I've just run PhotoRec and the code given as a way
Given the following code from a Microsoft example: public class EngineMeasurementCollection : Collection<EngineMeasurement> {
Given this XML file: <?xml version=1.0 encoding=UTF-8?> <root> <data> <track clipid=1> <url>http://www.emp3world.com/to_download.php?id=33254</url> <http_method>GET or
how to make a good MySQL Database Table Structure from this XML-Code? <xml> <product>
I've worked on many projects where I've been given code by others to update.

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.