coolaj86 / formaline

formaline is a module for handling form requests ( HTTP POSTs / PUTs ) and for fast parsing of file uploads.
Apache License 2.0
239 stars 16 forks source link

How to use formaline with express? #19

Closed g-eorge closed 13 years ago

g-eorge commented 13 years ago

Sorry if this is not the right place to ask this.

I'm trying to adapt the simple upload example into an express app using express-resource routing. More specifically into a 'custom action'.

See relevant code: https://gist.github.com/1184010

I use curl to upload the file and about 2 in 3 requests have the following error. So I am wondering if something in express is doing something to the request before formaline gets to it?

Failed to parse request data . Request was empty or already completed .

Full log:


 formaline, event "message" -->  {"type":"warning","isupload":true,"msg":"unable to retrieve session identifier, function getSessionID( req ) does not return a String!"}  
 formaline, HTTP Request Headers : {
    "host": "localhost:3000",
    "connection": "keep-alive",
    "referer": "http://localhost:3000/media/4e5ce54691d12ea15200001b/new_upload.html",
    "content-length": "75534",
    "cache-control": "max-age=0",
    "origin": "http://localhost:3000",
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1",
    "content-type": "multipart/form-data; boundary=----WebKitFormBoundarydnriwVzLSBPGmFzA",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "accept-encoding": "gzip,deflate,sdch",
    "accept-language": "en-US,en;q=0.8",
    "accept-charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3"
} 
 formaline, event "message" -->  {"type":"warning","isupload":true,"msg":"Failed to parse request data . Request was empty or already completed ."}  
 <------------------------------------------------> 
  (°)--/PARSER_STATS/  
   |                           
   |- overall parsing time    : 0.0000 secs  
   |- chunks received         : 0 
   |- average chunk rate      : NaN chunk/sec 
   |- average chunk size      : NaN KBytes 
   |- data parsed             : 0.0000 MBytes 
   |- average data rate       : NaN MBytes/sec 
   | 
  (°)--/POST_OVERALL_RESULTS/  
   |                           
   |- overall time            : 0.242 secs 
   |- bytes allowed           : 1024.000000 MBytes 
   |- data received           : 0.000000 MBytes 
   |- data written to disk    : 0.000000 MBytes 
   |- average throughput      : 0.000 Mbit/s 
   |- completed files         : 0 
   |- partially written files : 0 
 <------------------------------------------------>

Post Done..

Any ideas how to fix this?

rootslab commented 13 years ago

Hi g-eorge!

Sorry but I don't use express.js! However, you are right, probably express.js empties the request payload before that formaline can parse it, maybe a misconfiguration or bug .

I think is better that you contact directly @visionmedia for a suggestion on how to use express.js together with formaline! ( however I am here if you need further help ) .

P.S. Note that If express.js uses connect-form for handling multipart / form-data it uses formidable behind the scenes.

tj commented 13 years ago

i'd have to see the middleware setup

rootslab commented 13 years ago

Hi @visionmedia, it is a weird behaviour, time ago seems that another user have a similar issue with expressjs + formidable / formaline . I have modified formaline to handle this situation, but I have no idea of why should have emptied the request . You have some idea about it ?

tj commented 13 years ago

it doesnt, but it is possible that they have middleware that do something async and do not "pause" the request events

rootslab commented 13 years ago

I think the same thing. Thanks for now! ;)

g-eorge commented 13 years ago

Hi again @rootslab and @visionmedia, I have just finished extracting the problem into a simpler format so it can be more easily reproduced. I have found that the problem seems to be something that mongoose is doing, not express or formaline. I will ask the mongoose folks if they have any ideas.

In case you are interested I put a sample express / formaline application here:

https://github.com/g-eorge/express-formaline-example

On the master branch there is no mongoose and formaline seems to be working fine (I am not sure if I have done things in the best way with express, but it seems to work).

On the mongoose branch the file upload doesn't work.

Relevant instructions can be found in the Readme on each branch.

Thanks for your earlier advice.

tj commented 13 years ago

@g-eorge it's because you're doing something async (querying) before parsing the upload, so without "pausing" (buffering) the "data" events you're losing them