从角度控制器发布到节点

时间:2017-07-19 15:32:06

标签: javascript angularjs node.js express

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

我有一个角度控制器,可以发布到我的节点服务器。

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

  

POST http://www.mysite.co.uk/mm3/back-end/savephotos 404(未找到)

我老实说丢了,我已经把这个帖子重写了5次,我找不到问题。

如果有人能看到我出错的地方,请提供帮助。

角度控制器

立方毫米\ 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);

然后我的节点后端

更新:  所以我在我的函数中添加了一些控制台日志,以查看它出错的地方以及它的位置。 我一直在:

  

测试1功能启动错误保存照片

立方毫米\后端\ 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);
            }
        });
    });
}

2 个答案:

答案 0 :(得分:1)

我认为这是问题所在:

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

将其更改为

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

答案 1 :(得分:1)

在Angular中,下面是:

$http.post('back-end/savephoto......

变为:

$http.post('/back-end/savephoto.....

在Node中,如下:

app.post('back-end/savephoto.....

变为:

app.post('back-end/savephoto....

然后,您需要在Node路由下添加 console.log 以查看它是否已执行。这将缩小范围。此外,您可以删除超时之外的 $ http.post 调用,以消除明显的情况。

让我知道你是怎么过的。

砂眼