Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Request Stream Event can not work with POST mode #4

Open
PatrickSCLin opened this Issue · 0 comments

1 participant

@PatrickSCLin

hi there, I just occurred a problem now,
( express or restify ) seems could not triggered request stream event anymore
after set a " asynchronous verification "

here is the simple demo which I test.

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');
});

in normal way, the console should print like:

server is running
loading
loading
loading
load end
contentLength: 355968
dataLength: : 355968

(content-length should match with the real data-length which we got)
(but in this simple code, most time can not triggered stream event, or data-length would be loss)

but if use asynchronous verification, console won't print any info from the stream event,
even it would get fail if we try to using req.pipe() to upload the data to amazon s3 by know.
and without asynchronous verification, I can not get account info from mongodb.

is there any way to verify account by asynchronous way and make sure request stream work fine ?
ps: What I Upload is a mp3 file which about 300kbs, I had try this issue with jpg image too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.