Expressjs bodyParser和connect-form

时间:2011-12-12 21:47:56

标签: javascript node.js express

我使用连接表单上传图片。但如果我使用bodyParser(),它就不起作用。 反过来说,如果我不使用bodyParser,我无法上传文件?

如何让他们一起玩?这是我的配置:

app.configure(function() {
    app.register('.html', require('ejs'));
    app.set('views', __dirname + '/../views');
    app.set('view engine', 'html');
    app.use(gzippo.staticGzip(__dirname + '/../public'),{ maxAge: 86400000 });
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(form({ 
        keepExtensions: true,
        uploadDir: __dirname + '/../tmp'
    }));
    app.use(express.cookieParser());
    app.use(express.session({
        secret: 'test',
        cookie: { secure: true },
        store: new MySQLSessionStore(client.database, client.user, client.password)
    }));
    app.use(expressValidator);
    app.use(app.router);
    app.use(express.csrf());
});

1 个答案:

答案 0 :(得分:5)

如果您使用的是最新的Express,则不需要包含connect-form(自Connect 1.8.x以来不推荐使用)。

只需在你的路线中使用req.files来获取上传的文件,Express会完成其余的工作。看看这篇文章:

http://tjholowaychuk.com/post/12943975936/connect-1-8-0-multipart-support