I'm building a node.js app using the Bogart framework, and I would like to handle file uploads with Formidable.
Here's my HTML (copied more or less straight from the Formidable docs):
<form action="/upload" enctype="multipart/form-data" method="post">
<input type="text" name="title"><br>
<input type="file" name="upload" multiple="multiple"><br>
<input type="submit" value="Upload">
</form>
And here's my server code:
var bogart = require('bogart'),
formidable = require('formidable');
var router = bogart.router();
router.post('/upload', function(req) {
var form = new formidable.IncomingForm();
var res = bogart.res();
console.log("Beginning to parse files.");
form.parse(req, function(err, fields, files) {
console.log("Completed parsing files.");
res.setHeader('Content-Type', 'text/plain');
if (err) {
console.log("Upload error.")
res.status(500);
res.send('Error');
return res.end();
}
res.status(200);
res.send("Success.");
res.end();
});
return res;
});
var app = bogart.app();
app.use(bogart.batteries);
app.use(router);
app.start(8991, '127.0.0.1');
When I submit the data, my node server logs "Beginning to parse files."
, indicating it has received the POST, but "Completed parsing files."
, in the form.parse() callback, never gets logged, so I figure something must be going wrong with the parse. I get no other errors--the client just waits for a server response indefinitely.
My hunch is that I don't understand how to use middleware properly, but I'm also not sure if it's correct to call Formidable middleware, though it does appear to be in the middle of things...