Colin
Colin

Reputation: 1855

express.bodyParser not parsing?

I've stripped everything down to the absolute bare bones to try and fine out whats wrong, and still can't get bodyParser to do anything. It won't even work for .txt files.

server.js

var     cfg     = require(__dirname + '/config'),
    express     = require('express');


var app = express();

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.logger(cfg.LOGGER));
app.use(require('connect').bodyParser());
app.use(express.static(__dirname + '/public'));
app.use(express.favicon(__dirname + '/public/favicon.ico'));

app.get('/', function(req, res) {
    res.render('index');
    res.end();
});

app.post('/upload', function(req,res) {
    console.log(req.body.file);
    res.end();
});

app.listen(2017);

index.ejs

<form enctype="multipart/form-data" action="/upload" method="POST">
    <input id="multipart/form-data" type="file" name="foo" />
    <button>Upload</button>
</form>

the result is that req.body.file is undefined

Upvotes: 0

Views: 1471

Answers (2)

sachin
sachin

Reputation: 14355

Change this

  app.use(require('connect').bodyParser());
            as
  app.use(express.bodyParser());  


  app.post('/upload', function(req,res) {
     console.log(req.body.foo);//in req.body u have to mention the name of the file..
     res.end();
  });

Upvotes: 0

go-oleg
go-oleg

Reputation: 19480

You can access the file using req.files.<yourFileInputFieldName>:

console.log(req.files.foo);

More info in this section of the docs.

Upvotes: 2

Related Questions