1

I've followed the express multipart example to upload a file to server, and it works fine. But if I use a route middleware like this (coffeescript):

loadUser = (req, res, next) ->
    if req.session.user_id?
        db.user.findById req.session.user_id, (err, user) ->
            if user?
                req.currentUser = user
                next()
            else
                next()
    else
        next()

app.post '/file',loadUser, (req, res) ->
    req.form.complete (err, fields, files) ->
        if (err)
            console.error err
        else
            console.log '\nuploaded %s to %s', files.image.filename, files.image.path
    req.form.on 'progress', (bytesReceived, bytesExpected) ->
        percent = (bytesReceived / bytesExpected * 100) | 0;
        process.stdout.write 'Uploading: %' + percent + '\r'

The file doesn't upload and there's nothing log and no error. Then I clear the code in loadUser like this:

loadUser = (req, res, next) ->
    next()

It works fine again. Is there something wrong in my loadUser method?

0

1 Answer 1

1

I've found the problem, because when the program route to the '/file', the form.complete had been emitted in the loadUser middleware. So there's nothing log and no error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.