node.js - 用于多部分 Nodejs 的 Bodyparser

标签 node.js middleware body-parser

enter image description here enter image description here所以我想从我的 body 解析器开始,而且我正在使用“multer”

我的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 ?

最佳答案

问题是您正在发送多部分/表单数据请求但是您正在覆盖 Content-Type 并将其设置为不同类型(应用程序/x-www-form-urlencoded),这是一种完全不同的格式。如果删除此覆盖,应该没问题。

关于node.js - 用于多部分 Nodejs 的 Bodyparser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42260888/

相关文章:

javascript - 具有压缩功能的 Node.JS http 服务器 - 发送变量作为响应

python - 使用 JS 后端和 Python 进行机器学习

node.js - 访问 Mocha 中的 Meteor 设置

node.js - Node Express JS 无法解析 JSON 正文

node.js - Node : Is it possible to eval js code using runInNewContext and limit its execution time by a timeout?

node.js - app.get(/) 不渲染index.ejs 文件

javascript - 设置三个 express 实例 : admin, 站点和应用程序

node.js - 在将响应发送到客户端后执行的 express/connect 中间件

javascript - 添加 bodyParser() 挂起所有 api 请求

javascript - 如何使用带有导入的 Body Parser 而不是必需的? ES6