有效载荷字节与实际字节不同
Payload bytes different from actual bytes
我遇到了一个问题,当用户发送图像数据时,它保存在损坏的服务器上。
所以,我基本上有这个设置:
- api
. index.js
- methods
. users.js
(我已经删掉了与问题无关的文件)
在 API 文件之外,有一个 server.js
会在访问 api.example.com 时启动它。 (基本上就像一个 VirtualHost)
话虽这么说。问题是:
./api/index.js
(问题点)
// All this essentiall does is get the get the whole payload as a buffer.
req.on("data", function(chunk){
// Potential problem area.
req.api.payload = ((req.api.payload) ? (Buffer.concat([req.api.payload, chunk])) : (chunk));
});
req.on("end", function(){
// Check if we're on api.example.com/users/
if (path[0].match(/^users$/i)) {
methods.users(req, res);
}
});
我认为问题出在有效载荷的某个地方。
但是...以防万一,我将包括 ./api/methods/users.js
...(仅包括可能出现问题的部分,因为它是一个大文件)
./api/methods/users.js
else if (req.method.match(/^POST$/i)) {
if (!path[2] || path[2].match(/^(?:info|data)$/i)) {
// ... (Everything that was here didn't apply to the image upload)
} else if (path[2].match(/^(?:pic|picture|avatar)$/i)) {
// Url matches api.example.com/users/<user_id>/pic
var __base = "/home/user/web/hosts/resources/static/images/api/users/"; // Upload path
// Another potential spot where the problem could be.
fs.writeFile(__base + path[1] + ".png", req.api.payload, "binary", function(err){
if (err) res.api.end(err);
res.api.end(req.api.payload.slice(0, 40)); // Testing
});
}
}
请注意,这只是为了测试上传,我知道有人可以上传其他数据,但这是本地的。目前只是为了测试上传的数据。
另外请注意,任何您没有看到明确定义的变量都与问题无关,只是没有:
path
:Url 作为数组,例如"/user/xero/info"
-> ["user", "xero", "info"]
req.api
:之前创建的对象(在主 server.js
文件中)。
请注意,我正在通过 cURL 命令上传文件:
curl -X POST -d @random.png http://api.poweredrails.org/users/test4/pic
问题出在您的 curl
用法上,您需要指定 --data-binary
而不仅仅是 -d
。您还应该设置 Content-Type
header 以便 curl 做正确的事情:
curl -X POST -H "Content-Type: image/png" --data-binary @random.png http://api.poweredrails.org/users/test4/pic
另一种选择是支持在您的后端应用程序中使用 multipart/form-data,尤其是在想要随图像提交其他信息时。那么您的 curl 用法将如下所示:
curl -X POST --form "mypicture=@random.png" --form "foo=bar" http://api.poweredrails.org/users/test4/pic
我遇到了一个问题,当用户发送图像数据时,它保存在损坏的服务器上。
所以,我基本上有这个设置:
- api
. index.js
- methods
. users.js
(我已经删掉了与问题无关的文件)
在 API 文件之外,有一个 server.js
会在访问 api.example.com 时启动它。 (基本上就像一个 VirtualHost)
话虽这么说。问题是:
./api/index.js
(问题点)
// All this essentiall does is get the get the whole payload as a buffer.
req.on("data", function(chunk){
// Potential problem area.
req.api.payload = ((req.api.payload) ? (Buffer.concat([req.api.payload, chunk])) : (chunk));
});
req.on("end", function(){
// Check if we're on api.example.com/users/
if (path[0].match(/^users$/i)) {
methods.users(req, res);
}
});
我认为问题出在有效载荷的某个地方。
但是...以防万一,我将包括 ./api/methods/users.js
...(仅包括可能出现问题的部分,因为它是一个大文件)
./api/methods/users.js
else if (req.method.match(/^POST$/i)) {
if (!path[2] || path[2].match(/^(?:info|data)$/i)) {
// ... (Everything that was here didn't apply to the image upload)
} else if (path[2].match(/^(?:pic|picture|avatar)$/i)) {
// Url matches api.example.com/users/<user_id>/pic
var __base = "/home/user/web/hosts/resources/static/images/api/users/"; // Upload path
// Another potential spot where the problem could be.
fs.writeFile(__base + path[1] + ".png", req.api.payload, "binary", function(err){
if (err) res.api.end(err);
res.api.end(req.api.payload.slice(0, 40)); // Testing
});
}
}
请注意,这只是为了测试上传,我知道有人可以上传其他数据,但这是本地的。目前只是为了测试上传的数据。
另外请注意,任何您没有看到明确定义的变量都与问题无关,只是没有:
path
:Url 作为数组,例如"/user/xero/info"
->["user", "xero", "info"]
req.api
:之前创建的对象(在主server.js
文件中)。
请注意,我正在通过 cURL 命令上传文件:
curl -X POST -d @random.png http://api.poweredrails.org/users/test4/pic
问题出在您的 curl
用法上,您需要指定 --data-binary
而不仅仅是 -d
。您还应该设置 Content-Type
header 以便 curl 做正确的事情:
curl -X POST -H "Content-Type: image/png" --data-binary @random.png http://api.poweredrails.org/users/test4/pic
另一种选择是支持在您的后端应用程序中使用 multipart/form-data,尤其是在想要随图像提交其他信息时。那么您的 curl 用法将如下所示:
curl -X POST --form "mypicture=@random.png" --form "foo=bar" http://api.poweredrails.org/users/test4/pic