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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:44:58+00:00 2026-06-15T00:44:58+00:00

How do request streams works with node.js (express or restify) ? When a client

  • 0

How do request streams works with node.js (express or restify) ?

When a client who tries to upload an audio, mpeg or other binary file to the server, the request should be a Readable Stream on the server. that we could pipe into another stream using request.pipe() to for example get the file from the request, and then upload the file to amazon s3 using knox.

When I’m using an asynchronous authentication method part of the streamed data is being lost and the length doesn’t match with the transmitted content-length header anymore. Is there any way to avoid this behavior?
Is the data of request stream stored only in memory or does node.js store the data in some local temp folder?

var express = require('express'),
app = express(),
passport = require('passport'),
BasicStrategy = require('passport-http').BasicStrategy;

var users = [
    { id: 1, username: 'bob', password: 'secret', email: 'bob@example.com' }
    , { id: 2, username: 'joe', password: 'birthday', email: 'joe@example.com' }
];

function findByUsername(username, fn) {
    for (var i = 0, len = users.length; i < len; i++) {
        var user = users[i];
        if (user.username === username) {
            return fn(null, user);
        }
    }
    return fn(null, null);
}

passport.use(new BasicStrategy(
    function(username, password, done) {
        process.nextTick(function () {
            findByUsername(username, function(err, user) {
                if (err) { return done(err); }
                if (!user) { return done(null, false); }
                if (user.password != password) { return done(null, false); }
                return done(null, user);
            })
        });
    }));

app.configure(function() {
    app.use(express.logger());
    app.use(passport.initialize());
    app.use(app.router);
});    

app.post('/upload', 
    passport.authenticate('basic', { session: false }),
    function(req, res, next) {

        var dataLength = 0;

        req.on('data', function(chunk) {
            console.log('loading');
            dataLength += chunk.length;

        }).on('end', function() {
            console.log('load end');
            console.log('contentLength: %s', req.headers['content-length']);
            console.log('dataLength:  : %s', dataLength);
            res.send(200);

        }); 
    });

app.listen(8080, function() {
    console.log('server is running');
});
  • 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-15T00:44:59+00:00Added an answer on June 15, 2026 at 12:44 am

    Lets see, where to begin:

    • Yes, the request is a ReadStream that will emit data events as you said.
    • The first chunk of data in the stream is stored in memory, there are no temporary files.

    The general issue you are running into is that you need to capture the data emitted from the req and re-emit it once the authentication is completed. Connect provides helpers to do this for you. It looks like passport-http doesn’t currently use them.

    If you install the pause module it can handle this for you.

    Try something like this:

    var pause = require('pause');
    
    // Create our own middleware constructor.
    var bufferedAuthenticate = function(){
        // Set up standard authenticate handler passing arguments through.
        var authMiddleware = passport.authenticate.apply(passport, arguments);
    
        // Pass our own middleware instead that wraps passport-http.
        return function(req, res, next){
            // Pause the request before authenticating.
            var obj = pause(req);
            authMiddleware(req, res, function(err){
              next(err);
    
              // Emit any cached data events that fired while authenticating.
              obj.resume();
            });
        };
    };
    
    app.post('/upload',
        bufferedAuthenticate('basic', { session: false }),
        function(req, res, next) {
            var dataLength = 0;
    
            req.on('data', function(chunk) {
                console.log('loading');
                dataLength += chunk.length;
    
            }).on('end', function() {
                console.log('load end');
                console.log('contentLength: %s', req.headers['content-length']);
                console.log('dataLength:  : %s', dataLength);
                res.send(200);
            }); 
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have response stream from a ftp web request that returns binary file. I
I am trying to upload file to sharepoint. Authorization works fine, but it ends
Request.Form(ListBox1.ClientID) returning nothing in webcontentform and usercontrols . But it works perfectly fine with
From some page I launch a GET request to some ASHX handler that streams
BOSH (Bidirectional-streams Over Synchronous HTTP) is a sneaky way of implementing 2-way client-server communication
I have the following code to retrieve a file using FTP (which works fine).
I am using Node's basic http.request() function without problem on normal HTTP servers. I
My upload servlet keeps throwing me an exception saying that the file that I'm
Request[key] vs Request.Params[key] vs Request.QueryString[key] Which method do you seasoned programmers use? and why?
Request my test webService for the data, The tableView's last row is More data...

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.