1

Ok, I do not understand what is going here, works locally but not on my server.

I have a angular controller that post to my node server.

each time I try and run the function that triggers the post I get

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

Im honestly lost, ive rewritten the post 5 times I cant find the problem.

If anyone can see where ive gone wrong please help.

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);

then my node back-end

UPDATED: So I have added a few console logs in my function to see where its going wrong and where it is getting to. I keep getting:

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);
            }
        });
    });
}
2
  • What is supposed to be contained within photos[counts] ? I cannot see the data this relates to. Commented Jul 19, 2017 at 15:46
  • @FreddieColeman have updated the code with more information Commented Jul 20, 2017 at 10:22

2 Answers 2

1

I think this is the problem:

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

change it to

app.post('/back-end/savephoto', function (req, res) {
        // skipped some lines
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Try changing both routes so that each has a beginning slash. Additionally, make sure that you're actually pointed at the localhost port (I assume you're running node locally). Maybe try appending localhost:xxxx to the $http.post route.
@WillCarron im on a live server, was working fine locally but live server it dose not work
1

In Angular, the below:

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

Becomes:

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

In Node, the below:

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

Becomes:

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

Then, you need to add a console.log under the Node route to see if it even is executed. This will narrow it down. Also, you can remove the $http.post call outside of the timeout to eliminate the obvious.

Let me know how you get on.

Shayan

4 Comments

Thanks Shayan, will try this morning and let you know
But you should get something in console right? If you are not getting it there, then there is a routing issue. So, anything in console?
Hey Shayan, so Ill Update my question in a sec with updated node code. In my console I get "error saving photo" updating now
That is an entirely new issue. You have reworded the whole question to mean something else. The original question you posted has been solved. Close this and open up a new one. Furthermore, I suggest you clean up your code if you are going to ask on SO. Make sure it is stripped down to the bare minimum which demonstrates the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.