The Issue: Resulting uploaded json file contains the curl boundary information.
Requirement: file posted should be exactly the file uploaded on the server side.
Stack: Busboy (https://github.com/mscdex/busboy), Node, Express, cURL
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/foo', function(req, res) {
// some logic
if (req.busboy) {
req.pipe(req.busboy);
file.pipe(fs.createWriteStream(fpath));
fstream.on('close', function() {
deferred.resolve(fpath);
});
//more logic
}
});
curl http://localhost:3000/foo -v -X POST -F "file=@file.json"
Update:
I went with a module called multer
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
router.post('/upload', upload.single('file'), function (req, res) {
if (!req.file) {
throw 'No file
}
res.send('success. file is at: %s', req.file.path);
});
/*
works with curl request like so:
curl http://localhost:3000/upload \
-X POST \
-F "file=@npm-shrinkwrap.json"
*/