问题
I have such code, that updates some studio info and, if it's needed, download new image.
postupdate: function(req,res){
sails.log.info("Reporting from [studio/postupdate]");
var id = req.param('id');
var uploadFile = req.file('image');
Studio.findOne({id: id}).exec(function(err, studio){
if(err) return res.negotiate(err);
studio.name= req.param('name');
studio.description = req.param('description');
sails.log.debug(uploadFile._files.length);
if(uploadFile._files.length){
sails.log.debug("Trying to download image");
var oldName = uploadFile._files[0].stream.filename;
var newName;
uploadFile.upload(
{
dirname: publicImgDir,
saveAs: oldName
},
function (err, files) {
if (err) return res.negotiate(err);
var fd = fs.createReadStream(publicImgDir+oldName);
var hash = crypto.createHash('sha256');
hash.setEncoding('hex');
fd.on('end', function () {
hash.end();
newName= hash.read()+Path.extname(oldName);
sails.log.debug("Trying to rename downloaded file");
fs.rename(publicImgDir+oldName, publicImgDir + newName, function (err) {
if (err) {
sails.log.debug("Trying to rename downloaded file[2]");
fs.unlink(publicImgDir+oldName);
fs.rename(publicImgDir+oldName,publicImgDir + newName);
}
sails.log.debug("Renaming complete");
sails.log.debug("Trying to copy file to origin assets directory");
Utilities.copyFile(publicImgDir+newName,
originImgDir+ newName,
function(err){if(err) sails.log.debug(err);});
studio.image = newName;
studio.save(function(err){
return res.redirect('admin/studio/update/'+id);
});
});
});
fd.pipe(hash);
});
}else{
studio.save(function(err){
if (err) {
return res.send(500);
}
return res.redirect('/admin/studio/update/'+id);
});
}
});}
If i edit all fields (including image), everything is ok. But if i'd not select image - everything runs like it should, update perform successfully and code redirect like it should. Then goes this error:
events.js:141
throw er; // Unhandled 'error' event
^Error: EMAXBUFFER: An Upstream (`NOOP_image`) timed out before it was plugged into a receiver. It was still unused after waiting 4500ms. You can configure this timeout by changing the `maxTimeToBuffer` option.
at null.<anonymous> (........\npm\node_modules\sails\node_modules\skipper\standalone\Upstream\Upstream.js:86:15)
at Timer.listOnTimeout (timers.js:89:15)
If i comment everything that belongs to image download - no error showing.
回答1:
In documentation: https://sailsjs.com/documentation/reference/configuration/sails-config-http
To customize Skipper, first make sure to npm install skipper --save in your app. Next, uncomment the following code in your config/http.js file:
bodyParser: (function _configureBodyParser(){
var skipper = require('skipper');
var middlewareFn = skipper({
strict: true,
maxTimeToBuffer: 100000
});
return middlewareFn;
})(),
来源:https://stackoverflow.com/questions/32957779/emaxbuffer-error