Search code examples
angularjsnode.jsmulter

when trying to upload a file getting 404 not found error node js


this is my code:

angular.module('fileModelDirective',[])

.directive('fileModel',['$parse',function ($parse) {
return {
    restrict: 'A',
    link: function (scope, element, attrs) {
        var parsedFile = $parse(attrs.fileModel);
        var parsedFileSetter = parsedFile.assign;
        element.bind('change', function () {
            scope.$apply(function () {
                parsedFileSetter(scope, element[0].files[0]);
            });
        });
    }
};
}]);

and in server I have:

 app.post('/upload', function (req, res) {
    upload(req, res, function (err) {
        if (err) {
            if (err.code === 'LIMIT_FILE_SIZE') {
                res.json({success: false, message: 'The image file Size is too large, Max size allowed is 10MB'});
            } else if (err.code === 'filetype') {
                res.json({success: false, message: 'The image file type is invalid, Must be .png/.jpeg/.jpg'});
            } else {
                res.json({success: false, message: 'File was not uploaded'});
            }
        }
        else{
            if(!req.file){
                res.json({success: false, message: 'No file selected'});
            }else{
                res.json({success: true, message: 'The file was uploaded'});
            }
        }
    });
});

But I keep getting this error:

:63342/upload Failed to load resource: the server responded with a status of 404 (Not Found)

what is the problem?


Solution

  • A 404 error is an error from the client. It means the server does not found what you are asking for.

    404 Not Found

    The requested resource could not be found but may be available in the future. Subsequent requests by the client are permissible.

    Generally, it happens when you misspelled your API, or try to request something that does not exist. Check if your correctly asking to the correct server, and if it accepts HTTP POST on /upload ; and if it correctly accept the file.