我有一个 node.js 应用程序,目前仅支持 x-www-form-urlencoded 请求。如果有人需要将文件作为附件发送,我必须扩展对表单数据的支持。目前,从请求中读取数据是使用 DecodeURIComponent 完成的,看起来类似于以下内容。是否可以调整它来读取表单数据?
exports.parseUrlEncodedBody = function(event) {
//This is to extract url encoded data
var temp = {};
if (event.body) {
// retrieve keys & values
var pm = event.body.split("&");
// store keys and values in temp object
params.forEach(function (item, index, array) {
var keyValue = item.split("=");
var key, value;
if (keyValue.length >= 1) {
key = decodeURIComponent(keyValue[0]);
if (keyValue.length >=2) {
value = decodeURIComponent(keyValue[1]);
} else {
value = "";
}
temp[key] = value;
}
});
}
最佳答案
Is it possible to adapt this to read form data?
没有。数据格式完全不同。您需要从头开始重写它。
考虑使用诸如multer之类的模块就是为此而设计的。
如果你想从头开始编写自己的库,那么你应该引用 RFC 7578 ,它描述了数据格式。
关于javascript - 如何在node.js中读取多部分表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45545283/