I'm using express node framework with multipart middleware. Setting the multipart defer: true
option gets me access to req.form
in the route but it does not seem to be a valid instantiation of the multiparty Form Object. It's just a plain old JS Object.
The issue is that I cannot use the features outlined in multiparty's documentation on the Form Object. Listening for events, parsing the form, etc. are not working.
I'm using this as recommended instead of the depreciated bodyParser() with express:
app.use(express.urlencoded());
app.use(express.json());
app.use(express.multipart({ limit: '900mb', defer: true }));
In my route:
app.post('/api/videos/upload', isLoggedIn, function(req, res) {
var form = req.form;
form.on('progress', function(bytesReceived, bytesExpected) {
console.log(bytesReceived + ' / ' + bytesExpected);
});
I get nothing in the logs about progress. All the examples I see assume this form object is the real deal and not just a data object. What are they doing differently? What am I missing?
I have seen solutions that suggest writing my own middleware that use multiparty directly. I considered this but I'm using the multipart middleware for other forms at different routes that do not need this level of control. Is it possible to use multipart and write my own middleware just for uploads?
In case I'm going down a rabbit hole I'll explain my ultimate goal. I want to check the expected bytes before the upload starts so I can kick it back to the front end immediately. No reason the user should wait for 900MB to upload before finding out the file is too big. If anyone has any insight on how to achieve that some other way please share.