我在Node中创建了一个函数,用于将本地存储映像保存到我服务器上的本地文件中。它似乎在我的本地工作正常,但现在我已经上传到服务器,我似乎一直在收到错误。
在我的控制台中,我获得了一些控制台日志,我已添加它以帮助我调试,到目前为止我的控制台上写着:
请求
测试1功能已启动
测试1功能结束
保存照片功能base64 start
保存照片功能启动if else
错误保存照片
无论我改变什么,我都会得到error saving photo
任何人都可以在我的代码中看到为什么我一直收到此错误?任何帮助都非常感谢我已经在这个错误数小时了。
完整代码
app.post('/mm3/back-end/savePhoto', function (req, res) {
console.log('test 1 function started');
var folder = Math.random().toString(36).substr(2, 20);
var photos = req.body;
var counts = 0;
console.log('test 1 function ended');
var callback = function(counts){
console.log('start if else');
if(counts < photos.length){
saveBase64(photos[counts],folder,counts,callback);
console.log('test if line');
}else{
var counts = 0;
var response = {"folder":folder, "photos": photos.length};
console.log('test else line');
res.send(response)
}
console.log('end of function');
};
saveBase64(photos[counts],folder,counts,callback);
});
app.post('/mm3/downloadZip', function(req, res){
console.log('test zip file started');
var photos = req.body;
var out = photos[0];
var test = out.split('/');
var loc = test.pop();
var end = test.join('/');
console.log('test 3 function Generate zip file');
console.log(end);
var outName = '/' + end +'/mm3/MockUp.zip';
var output = fs.createWriteStream(outName);
var archive = archiver('zip', {store: true });
var zip = function(photos, f){
for(var t = 0; t < photos.length; t++){
var file = 'mockUp'+ t +'.jpg';
var from = '/var/www/html' + photos[t];
archive.file( from, { name: file });
}
f();
};
output.on('close', function() {
var photos = req.body;
var out = photos[0];
var test = out.split('/');
var loc = test.pop();
var end = test.join('/');
res.send(end + '/MockUp.zip');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.on('error', function(err) {
throw err;
});
archive.pipe(output);
zip(photos, f);
function f(){
archive.finalize();
}
});
function saveBase64(photo,folder, counts, callback){
console.log('save photo function base64 start');
var result = photo.split(',')[1];
var path = '/mm3/tmp/' + folder;
var filename = path + "/out"+ counts + ".png";
mkdirp( path, function() {
fs.writeFile(filename, result, 'base64', function(error){
console.log('save photo function start if else');
if (error) {
console.log('error saving photo');
}else{
console.log('photo saved');
counts ++;
callback(counts);
}
});
});
}
更新:
我现在正在
错误:请求实体太大 在readStream(/var/www/html/mm3/node_modules/raw-body/index.js:196:17) at getRawBody(/var/www/html/mm3/node_modules/raw-body/index.js:106:12) 在阅读(/var/www/html/mm3/node_modules/body-parser/lib/read.js:76:3) 在Layer.jsonParser [作为句柄](/var/www/html/mm3/node_modules/body-parser/lib/types/json.js:127:5) 在trim_prefix(/var/www/html/mm3/node_modules/express/lib/router/index.js:226:17) at /var/www/html/mm3/node_modules/express/lib/router/index.js:198:9 在Function.proto.process_params(/var/www/html/mm3/node_modules/express/lib/router/index.js:251:12) 在下一个(/var/www/html/mm3/node_modules/express/lib/router/index.js:189:19) 在Layer.expressInit [作为句柄](/var/www/html/mm3/node_modules/express/lib/middleware/init.js:23:5) 在trim_prefix(/var/www/html/mm3/node_modules/express/lib/router/index.js:226:17) at /var/www/html/mm3/node_modules/express/lib/router/index.js:198:9 在Function.proto.process_params(/var/www/html/mm3/node_modules/express/lib/router/index.js:251:12) 在下一个(/var/www/html/mm3/node_modules/express/lib/router/index.js:189:19) 在Layer.query [作为句柄](/var/www/html/mm3/node_modules/express/lib/middleware/query.js:37:5) 在trim_prefix(/var/www/html/mm3/node_modules/express/lib/router/index.js:226:17) at /var/www/html/mm3/node_modules/express/lib/router/index.js:198:9 在Function.proto.process_params(/var/www/html/mm3/node_modules/express/lib/router/index.js:251:12) 在下一个(/var/www/html/mm3/node_modules/express/lib/router/index.js:189:19) 在Function.proto.handle(/var/www/html/mm3/node_modules/express/lib/router/index.js:234:5) 在Function.app.handle(/var/www/html/mm3/node_modules/express/lib/application.js:119:16) 在Server.app(/var/www/html/mm3/node_modules/express/lib/express.js:28:9) 在emitTwo(events.js:87:13)