9

I want to parse a multipart form twice: once to grab the incoming fields, and later to process the file upload.

I am trying to maintain a proper separation of concerns within my Node app:

  • Controller is responsible for handling the incoming fields.
  • Model is responsible for the upload file logic.

I need to pass the fields data into the model to create a new instance, so the fields data needs to be available before the file upload starts.

Currently every form.parse() or equivalent function parses both fields and files together. Example: req.pipe(busboy) handles both file and fields together.

I have checked modules like node-multiparty, formidable, busboy, multer. Nobody seems to have a solution for this.

An example of what I would like to achieve is here: https://stackoverflow.com/questions/22336177/node-js-busboy-parse-fields-and-files-seperatly

Is this even possible?

4
  • 2
    wait...so you're going to have the Model be responsible for logic and control? Commented Jan 3, 2016 at 18:32
  • Good catch, normally that architecture is bad to mix up the concerns. This was 2 years ago so I don't exactly remember what problem I was solving when I was asking this question, but I do remember that I wanted the save logic to be part of the Mongoose schema object. By having the mongoose method on the model, I could easily save from anywhere in my app. Looking back, I don't think that the save method is generic enough to be a model method, probably should keep it as a separate service method. mongoosejs.com/docs/guide.html
    – Scott
    Commented Jan 4, 2016 at 20:38
  • 1
    lol i hadn't realized how old this was...it somehow flowed to the top of my "needs answering" queue. and yeah, I think it's correct to keep persistence operations separate from the data being persisted ESPECIALLY when those objects may be transferring data in two dimensions. Makes it to easy to screw up and persist things outside of the controller. Commented Jan 5, 2016 at 4:22
  • @Scott " This was 2 years ago so I don't exactly remember what problem I was solving when I was asking this question" - then you may consider to delete this question since it is unlikely that it will be answered at all... Commented May 21, 2017 at 8:31

1 Answer 1

1

I want to answer this question:

Is it possible, to read the multipart field headers before their content?

When I look at the multipart rfc, I see this example:

From:  Nathaniel Borenstein <[email protected]> 
To: Ned Freed <[email protected]> 
Subject: Formatted text mail 
MIME-Version: 1.0 
Content-Type: multipart/alternative; boundary=boundary42 


--boundary42 
Content-Type: text/plain; charset=us-ascii 

...plain text version of message goes here.... 

--boundary42 
Content-Type: text/richtext 

.... richtext version of same message goes here ... 
--boundary42 
Content-Type: text/x-whatever 

.... fanciest formatted version of same  message  goes  here 
... 
--boundary42-- 

I note that the headers like Content-Type are located between the body parts. Thus I conclude, you can not all headers before all bodies.

Now to your question:

I want to parse a multipart form twice: once to grab the incoming fields, and later to process the file upload.

It depends what you mean by "parsing". There is some parsing involved when reading the HTTP message to know when it ends. The end has an additional -- at the end:

--boundary42--

Ideas for parsing twice:

  • I conclude, one should be able to copy the whole answer from the socket and do the parsing later.
  • You can read the headers of the file before the body of the file but not all headers of all files.

Is this even possible?

Yes, there are cases in which it is possible (when the file is the last thing you upload). I do not know if it is universally possible what you need as I do not know exactly what you want to do.

I hope this clarifies things. If this is not a complete answer or you dislike this, please tell us why because this might be valuable feedback for others trying to answer the question.

Not the answer you're looking for? Browse other questions tagged or ask your own question.