Houseman Houseman - 1 month ago 19
Javascript Question

How do I send a file from postman to node.js with multer

Windows

Express 4.12.4

Multer 1.0.1

Node v0.10.22

I'm trying to send a file to my node.js server using postman.

I'm attempting to follow the readme here

Here's what I'm sending with postman:

POST /ingest HTTP/1.1
Host: localhost:3000
Cache-Control: no-cache
Postman-Token: 69dd2497-2002-56ed-30a4-d662f77dc0b0
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW


Here's what it looks like:
enter image description here

Here's what it's hitting on node.js

var Ingest = require('../controllers/ingest.js');
var multer = require('multer');
var upload = multer({ dest: 'uploads/',fileFilter:function(req,file,cb){
console.log('file is',file)
cb(null,true);
}
});

module.exports = function (app) {
app.post('/ingest', upload.single('test'), function(req, res, next) {
console.log(req.body);
console.log(req.file);
Ingest.ingestData()
.then(function (response){
return res.status(200).json(response);
});
});
}


When I hit this route with postman I get
{}
for
req.body
and
undefined
for
req.file
.

What am I doing wrong?

Here's where I initialize the
app
that gets passed in to the route file:

var express = require('express');
var app = express();
var http = require('http');
var cfg = require('./config')();
var passport = require('passport');
var cors = require('cors');

var bodyParser = require('body-parser');

app.set('port', process.env.PORT || cfg.port);
var corsOptions = {
origin: "*",
allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'x-reset-token', 'x-invite-token', 'x-api-key', 'x-www-form-urlencoded'],
credentials: true
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize());
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});


Maybe something in there is doing it?

Edit:



I've even tried

var Ingest = require('../controllers/ingest.js');
var multer = require('multer');
var upload = multer({ dest: 'uploads/',fileFilter:function(req,file,cb){
console.log('file is',file)
cb(null,true);
}
}).single('csv');

module.exports = function (app) {

app.post('/ingest', function(req,res){
upload(req, res, function(err) {
if(err){
console.log(err);
}
console.log(req.body);
console.log(req.file);
Ingest.ingestData()
.then(function (response){
return res.status(200).json(response);
});
});
});

}


And that didn't help. It doesn't log anything for
err

Answer

In Postman screenshot, the file field name is missing. The key should be csv since Multer accepts single file with that name.

Comments