Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node.js sample doesn't work #17

Open
ChristophHaag opened this issue Apr 13, 2014 · 17 comments
Open

Node.js sample doesn't work #17

ChristophHaag opened this issue Apr 13, 2014 · 17 comments

Comments

@ChristophHaag
Copy link

First, the Readme is outdated. It says to do npm install express but this downloads express 4.0 now, but the code depends on express 3.x.

Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.

So it should say npm install express@3.x until it is fixed for 4.x.

Then the upload simply never finishes on the server side.

I actually first tried the node.js sample from resume.js and after it worked fine I tried to change the directory of the uploaded files and at this point point it did the same which the sample here does:

The line POST done <filename> doesn't appear on the server side log (only partly_done for all the chunks), but on the browser side it says fileSuccess FlowFile [...] complete. The samples/Node.js/tmp directory remains empty.

I haven't really tested more and I don't think I have much motivation right now to do it.

node v0.10.26
chromium Version 35.0.1916.17

@AidasK
Copy link
Member

AidasK commented Apr 14, 2014

Well yes, this is an old example and needs a good rewrite. Example could even be rewritten on other nodejs framework.
Maybe somebody could collaborate on this?

@thrownblown
Copy link
Contributor

i'm having a ton of issues with this too. i haven't found a good middleware to handle the multipart/formdata.

suggestions?

@thrownblown
Copy link
Contributor

i made a pull request to fix this. i did not update the read.me in the pull.... maybe i'll make another.

also!

i'm having an issue assembling the binary from the blobs, if you're into stackoverflow points, you could answer my question (and the two other unanswered questions that are essentially the same question): http://stackoverflow.com/questions/23449065/reassemble-binary-after-flow-js-upload-on-node-express-server

@AidasK
Copy link
Member

AidasK commented May 4, 2014

Reassembling all chunks is easy, just call this:

var stream = fs.createWriteStream(filename);
r.write(identifier, stream);

And that is it!

But other question is, when this method should be called?
Maybe then all chunks are uploaded and present at tmp folder.
https://github.com/flowjs/flow.js/blob/master/samples/Node.js/flow-node.js#L122
And another issue is solved.

But there is another issue with duplicate calls of done: #16
This can be solved by creating and locking the file, once all chunks exists.
Then call r.write(identifier, stream);
Then clean all chunks. https://github.com/flowjs/flow.js/blob/master/samples/Node.js/flow-node.js#L182
Then release the lock and close the file.

Same approuch is done in php server side library: https://github.com/flowjs/flow-php-server/blob/master/src/Flow/File.php#L102

Hope this helps, and I hope someone could collaborate and update node.js sample with these fixes.

@ralyodio
Copy link

ralyodio commented Jul 3, 2014

It worked for me (now that I'm on the right server). But there is no method for deleting.

@michaelryancaputo
Copy link

@chovy Any chance this solution could be added to the example? I'm trying to figure it out, I understand what is happening here, but I can't seem to produce the result.

@ralyodio
Copy link

I decided not to use flow.js infavor of a custom handler.

@michaelryancaputo
Copy link

@AidasK Any chance you can provide a more complete answer with an example?

It looks like you've been pasting the same solution in a number of places (stackoverflow), but don't really go into any depth to explain it.

@AidasK
Copy link
Member

AidasK commented Jul 22, 2014

It isn't me posting in stackoverflow with this solution, somebody else is trying to earn some points.

The reason, why I don't provide a normal nodejs solution, is because this sample is not written by me. And I hate how it looks https://github.com/flowjs/flow.js/blob/master/samples/Node.js/flow-node.js
This sample needs a rewrite.

If you are just trying to play with flow.js or Nodejs is not neccessary, you should try php library instead: https://github.com/flowjs/flow-php-server

@michaelryancaputo
Copy link

@AidasK strange about the SO person.. what a lame thing to do.
Anyway, I worked with this most of yesterday and came up with this, if anything, maybe it's a good starting point for someone more experienced than me:

exports.post = function (req, res, next) {

    flow.post(req, function(status, filename, original_filename, identifier) {

        console.log('status: '+ status, filename, original_filename, identifier);

        if(status==='done'){

            var s = fs.createWriteStream('./uploads/' + filename);
            s.on('finish', function() {

                res.send(200, {
                    // NOTE: Uncomment this funciton to enable cross-domain request.
                    //'Access-Control-Allow-Origin': '*'
                });

            });

            flow.write(identifier, s, {end: true});
        }

    });

};

@AidasK
Copy link
Member

AidasK commented Jul 22, 2014

This might work, but be careful with simultaneous uploads, because status done might occur multiple times for the same file.

@michaelryancaputo
Copy link

@AidasK You're right, i've only needed it for a single file. Thank you for the feedback.

@MilosStanic
Copy link
Contributor

Hi @AidasK and @flashpunk,
I applied the solution from StackOverflow. However, I don't know how to set the destination directory programatically.
I would like to write my files to directories in format /uploads/YYYY/mm/dd/filename .
However, changing the destination directory from './tmp/' to '/uploads' broke the code.

It would be really, really nice if we had a working example of flow-node.js .

Thanks.

@paulklemm
Copy link

@MilosStanic You can set the chunk directory by modifying these two lines in the example: https://github.com/flowjs/flow.js/blob/master/samples/Node.js/app.js#L1 and
https://github.com/flowjs/flow.js/blob/master/samples/Node.js/app.js#L6

To store the resulting file in a custom directory, I've added

var stream = fs.createWriteStream(UPLOAD_DIR + '/' + filename);
flow.write(identifier, stream);

here: https://github.com/flowjs/flow.js/blob/master/samples/Node.js/app.js#L19

Does this answer your question?

@MilosStanic
Copy link
Contributor

@powernap thanks, I solved my issues with Flow weeks ago. I don't really remember what I did. But I'm using Flow for my web app. And I like it.

@ptgamr
Copy link

ptgamr commented Nov 22, 2015

Hope that this implementation is helpful for you guys (re-write in ES6 & use Bluebird Promise)

https://gist.github.com/ptgamr/4d1d07b770321a138c91

@AidasK Do you think it's good enough for a pull request?

@AidasK
Copy link
Member

AidasK commented Nov 22, 2015

@ptgamr Looks awesome and clean. If you can, please update node.js sample with your code. I would be glad to accept it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants