我的客户端将图像文件发送到服务器。它工作5次然后突然停止。我很擅长使用溪流和管道,所以我不确定我做错了什么。
服务器代码
http.createServer(function(req, res) {
console.log("File received");
// This opens up the writeable stream to `output`
var name = "./test"+i+".jpg";
var writeStream = fs.createWriteStream(name);
// This pipes the POST data to the file
req.pipe(writeStream);
req.on('end', function () {
console.log("File saved");
i++;
});
// This is here incase any errors occur
writeStream.on('error', function (err) {
console.log(err);
});
}).listen(3000);
客户端代码
var request = require('request');
var fs = require('fs');
setInterval(function () {
var readStream = fs.createReadStream('./test.jpg');
readStream.on('open', function () {
// This just pipes the read stream to the response object (which goes to the client)
readStream.pipe(request.post('http://192.168.1.100:3000/test'));
console.log("Send file to server");
});
}, 1000);
答案 0 :(得分:0)
表现得像资源耗尽问题。不确定哪些调用抛出错误,哪些只是返回。服务器是否在第6次呼叫时连接?写入流是否打开?管道是否打开?
保存图像后尝试结束连接并关闭管道。也许关闭写入流,不记得节点垃圾是否收集文件描述符。
答案 1 :(得分:0)
我必须在服务器端执行以下操作才能完成这项工作:
res.statusCode = 200;
res.end();