我正在使用Express 3的最新版本。我已經閱讀了文檔,並且完全按照所寫,但它仍然不工作,因爲它假設。我提交後我的上傳目錄中的文件,但然後一切都停止並從app.post回調函數不會觸發。代碼:express 3.0 bodyParser無法正常工作
HTML玉
form(action="/upload", method="post", enctype="multipart/form-data")
input(type="file", name="image")
input(type='submit', value='submit')
App.js
var express = require('express')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, mongo = require('mongodb')
, Server = mongo.Server
, Db = mongo.Db
, routes = require('./routes')
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser({uploadDir:'./upload'}));
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});
app.post('/upload', function(req, res) {
console.log(req.files.image) // this doesn't fire at all - no matter what i write here
res.send(200) //doesn't run also
});
更現代的版本需要'VAR bodyparser =需要( 'bodyparser'); app.use(bodyparser.urlencoded({延長:真}));',當然,一個人必須先'NPM安裝體parser' – ekkis 2015-10-31 08:27:17