Expressjs bodyParser and connect-form

Expressjs bodyParser and connect-form

本文关键字:connect-form and bodyParser Expressjs      更新时间:2023-09-26

我用连接表单上传图片。但是如果我用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());
});

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

只需使用req。来获取上传的文件,Express会完成其余的工作。查看这篇文章:

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