gpt4 book ai didi

javascript - 从 Angular Controller 发布到 Node

转载 作者:行者123 更新时间:2023-11-30 15:10:21 30 4
gpt4 key购买 nike

好吧,我不明白这里发生了什么,在本地工作但在我的服务器上不工作。

我有一个发布到我的 Node 服务器的 Angular Controller 。

每次我尝试运行触发我得到的帖子的函数时

POST http://www.mysite.co.uk/mm3/back-end/savephotos 404 (Not Found)

老实说我迷路了,我重写了 5 次我找不到问题。

如果有人能看出我哪里出了问题,请帮忙。

Angular Controller

mm3\js\controller.js

 //all photos've been pushed now sending it to back end
$timeout(function () {
$http.post('back-end/savephoto', $scope.photosToPhp).then(function (success) {
$scope.generating = false;
$scope.generateBtn = 'Generate';
//creating mock up gallery
for (var x = 0; x < success.data.photos; x++) {
var file = '/mm3/tmp/' + success.data.folder + "/out" + x + ".png";
$scope.gallery.push(file);
}
$scope.photosToPhp = [];
}, function (error) {
});

}, 800);

然后是我的 Node 后端

更新: 所以我在我的函数中添加了一些控制台日志,以查看它哪里出了问题以及它到达了哪里。我不断得到:

test 1 function started error saving photo

mm3\back-end\controller.js

     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;
var callback = function(counts){
if(counts < photos.length){
saveBase64(photos[counts],folder,counts,callback);
console.log('test 2 save photo');
}else{
var counts = 0;
var response = {"folder":folder, "photos": photos.length};
console.log('test 3 save photo else');
res.send(response)
}
};
saveBase64(photos[counts],folder,counts,callback);
});

app.post('/mm3/downloadZip', function(req, res){
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){
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){
if (error) {
console.log('error saving photo');
}else{
console.log('photo saved');
counts ++;
callback(counts);
}
});
});
}

最佳答案

我认为这是问题所在:

app.post('back-end/savephoto', function (req, res) {
// skipped some lines
});

改成

app.post('/back-end/savephoto', function (req, res) {
// skipped some lines
});

关于javascript - 从 Angular Controller 发布到 Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45195064/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com