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 8102161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:10:30+00:00 2026-06-05T23:10:30+00:00

I recently read a blog in Nodejitsu and I am wondering how this piece

  • 0

I recently read a blog in Nodejitsu and I am wondering how this piece of code works.

var fs = require('fs'),
http = require('http'),
httpProxy = require('../lib/node-http-proxy');


module.exports = function (logging) {
  // Code here is run when the middleware is initially loaded.
  var logFile = fs.createWriteStream('./requests.log');

  return function (request, response, next) {
    // Code here is run on each request.
    if (logging) {
      logFile.write(JSON.stringify(request.headers, true, 2));
    }
    next();
  }
}

And the explanation given for this piece of code is:

This middleware is for very simple logging – it will write the headers of each request to a log file.

the above module exported can be used as,

httpProxy.createServer(
  require('./example-middleware')(true),
  8000, 'localhost'
).listen(9000)

How is the code in the above method with next() invoked in every request? The usage is pretty simple: require the above module and it gets invoked every time.

  • 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-05T23:10:32+00:00Added an answer on June 5, 2026 at 11:10 pm

    I’ll simplify the actual process but the gist of it:

    When a request comes in, node passes the request and response object to the first middleware in the middleware stack. If that middleware sends a response or closes the connection in any way, then subsequent middleware are not called. Otherwise, that middleware has to tell node it’s finished doing it’s job to keep on moving through the middleware stack, so you call next() within your middleware to tell it to continue processing middleware.

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

Sidebar

Related Questions

I recently read this blog entry by Hadi Hariri: That dreaded M in ASP.NET
I've recently read an article http://www.ravelrumba.com/blog/static-cookieless-domain/ about Serving Static Content from a Cookieless Domain
I recently read through Code Complete, and it recommends that I create a project
Recently I read a book( CleanCode ) in this book, FUNCTION SHOULD DO ONE
I recently read Scott Chacon's post Smart HTTP Transport , and I was hoping
I recently read the blog posts on Pushing Pixels that describe how to achieve
I recently read Michael C. Feathers' book Working effectively with legacy code and came
I've read two really interesting pieces of advice, recently: In the comments to this
I recently read an article talking about the Java annotations, and on this latter
I recently read about quicksort and was wondering whether it would be smart to

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.