简体   繁体   中英

Parse node.js express app, form enctype=“multipart/form-data” destroys request body?

i am building an app backend with parse, and i need to do some image uploading, but when i post to the app url with the form enctype being multipart/form-data, the body is empty?

This is the method which recieves the post:

// These two lines are required to initialize Express in Cloud Code.
var express = require('express');
var app = express();

// Global app configuration section
app.set('views', 'cloud/views');  // Specify the folder to find templates
app.set('view engine', 'ejs');    // Set the template engine
app.use(express.bodyParser());    // Middleware for reading request body

app.post('/add_station', function(req, res) {
       console.log(JSON.stringify(req.body));
       res.send(JSON.stringify(req.body));

});

req.body is empty when the form type is multipart/form-data?

If you're using Express 4.0 or higher, it no longer comes bundled with middleware. Meaning you need to require and install them manually.

Body-parser also no longer works with file uploads, so you need to use something like multer .

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer  = require('multer')

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(multer({
    dest: './uploads/'
}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM