用于多部分 Nodejs 的 Bodyparser

Bodyparser for multipart Nodejs

所以我想从我的正文解析器开始,而且我正在使用 'multer'

我的多个选项:

var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
    cb(null, '/root/Unicon-Oauth/Resources/profile_images/')
},
filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
}
});

var pfImage = multer({storage:storage});

正文解析器位于 server.js

app.use(bodyParser.urlencoded({extended:true,limit: '20MB',parameterLimit:10000}));
app.use(bodyParser.json());

我有这样的路线

router.post('/edit',[auth.isAuthenticated,pfImage.single('pImage')],actions.edit);

功能就是这样

function edit(req,res)
{
  console.log(req.body);
}

控制台日志输出:

Blockquote

{"------WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name":"\"_id\"\r\n\r\n58a4735cfa328b7e9eaf6a3a\r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name=\"city\"\r\n\r\nKayseri\r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\nali\r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name=\"\"\r\n\r\n\r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt--\r\n"}

如何将其解析为 req.body?

问题是您正在发送 multipart/form-data 请求 但是 您正在覆盖 Content-Type 并将其设置为 different类型(application/x-www-form-urlencoded),这是一种完全不同的格式。如果您删除此覆盖,应该没问题。