Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expressjs bodyParser and connect-form

Im uploading images with connect form. But it does not work if I use the bodyParser(). And the other way around, if i don't use the bodyParser, i cant upload files?

How do I make them play together? Here is my config:

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());
});
like image 807
georgesamper Avatar asked Dec 12 '11 21:12

georgesamper


People also ask

Is bodyParser deprecated?

“bodyparser is deprecated 2021” Code Answer's // If you are using Express 4.16+ you don't have to import body-parser anymore.

How do you use bodyParser with express?

To use the Text body parser, we have to write app. use(bodyParser. text()) and the Content-Type in your fetch API would be text/html . That's it, now your backend service will accept POST request with text in the request body.

What does bodyParser text () do?

bodyParser. text([options]) Returns middleware that parses all bodies as a string and only looks at requests where the Content-Type header matches the type option. This parser supports automatic inflation of gzip and deflate encodings.


1 Answers

If you are using the latest Express you don't need to include connect-form (which is deprecated since Connect 1.8.x).

Just use req.files in your routes to get the uploaded files, Express does the rest. Check out this post:

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

like image 50
alessioalex Avatar answered Sep 28 '22 06:09

alessioalex