我已经把所有东西都剥夺了绝对的骨头,试图弄清楚什么是错的,但仍然无法让bodyParser做任何事情。它甚至不能用于.txt文件。
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>
结果是req.body.file
为undefined
答案 0 :(得分:2)
答案 1 :(得分:0)
更改此
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();
});