Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to avoid the data of request stream loss after doing some authentication on node.js?
    primarykey
    data
    text
    <p>How do request streams works with node.js (express or restify) ?</p> <p>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 <code>request.pipe()</code> to for example get the file from the request, and then upload the file to amazon s3 using knox.</p> <p>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 <code>content-length</code> 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? </p> <pre><code>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 &lt; 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'); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload