I've tried everything recommended in How can I use body-parser with LoopBack? and yet still no luck.
My client app is in AngularJS and uses the ng-file-upload module, specifically like this:
Upload.upload({
url: apiUrl + '/Recipes/uploadImage',
file: $scope.picFile,
params: {
recipeId: newRecipe.id
}
})
.then(function(res) {
console.log('succes:', res);
}, function(err) {
console.log('error:', err);
}, function(evt) {
console.log('progress:', evt);
});
On the server (Loopback) side, I have made sure that server/middleware.json
has the middleware registered:
...
"parse": {
"body-parser#json": {},
"body-parser#urlencoded": {"params": { "extended": true }}
},
...
For good measure, although I'm not sure if body-parser is even needed in addition to multer (and body-parser is being required anyway because of being registered in middleware.json
), I've included these lines in server/server.js
:
var bodyParser = require('body-parser');
var multer = require('multer');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(multer().any()); // for parsing multipart/form-data
And I've also installed both:
npm install --save multer
npm install --save body-parser
Finally, my custom remote method looks like this:
Recipe.remoteMethod(
'uploadImage',
{
accepts: [
{ arg: 'req', type: 'object', http: { source: 'req' } },
{ arg: 'res', type: 'object', http: { source: 'res' } }
],
returns: {
arg: 'status', type: 'object', root: true
},
http: {verb: 'post'}
}
);
The actual function so far is just trying to get something to work with:
Recipe.uploadImage = function (req, query, cb) {
console.log('params:', req.file, req.recipeId);
console.log('body:', req.body);
... // And once I can get this I'm going to get the stream and pipe it to a remote storage container, but I need the file first!
The output from posted to the above is
params: undefined undefined
body: {}
Which sort of suggests that multer isn't really doing its thing, and I'm not really getting the parts of the multi-part form post?
Help!