简体   繁体   中英

NodeJS + AngularJS - after some time, I get net::ERR_CONNECTION_RESET

I'm using Express with NodeJS and AngularJS in front-end. After click here, add data there, update some record here, show a list there, I get net::ERR_CONNECTION_RESET . It's strange, after 8 or 9 navigations through the application, I got this error. Before this, the AJAX calls are doing fine.

An example of an AngularJS controller:

app.controller('GrupoController', ['$scope', '$location', '$routeParams', 'GrupoService', 
    function ($scope, $location, $routeParams, GrupoService) {

    $scope.carregarDependenciasMeusGrupos = function () {
        GrupoService.carregarDependenciasMeusGrupos().then(function (result) {
            $scope.data = {
                grupos: result.Data
            };
        });
    };

    $scope.editarGrupo = function(grupo) {
        $location.path('/editar-grupo/' + grupo.Id);
    };
}]);

And also an example of a HTTP call:

function ajax(url, async, method, params, isJson, showLoading) {
    var deferred = $q.defer();
    $http.post('http://localhost:3000' + url, params).success(function(result) {

        if (result.StatusCode === 403) {
            $location.path('/login');
        } else {
            if (result.Success) {
                deferred.resolve(result);
            } else {
                deferred.reject();
                if (result.ErrorMessage) {
                    alert(result.ErrorMessage);
                }

                if (result.RedirectTo) {
                    window.location.href = result.RedirectTo;
                }
            }
        }
    }).error(function(err) {
        deferred.reject();
    });

    return deferred.promise;
}

This is the approach I'm using with Express and NodeJS to create an action that is called through that AJAX method above:

app.post('/grupos/get-dependencies-to-grupos', app.ensureAuthentication, function(request, response, next) {
    query.openConnection().then(function(connection) {

        var gruposRepository = new repositories.Grupos(connection);
        gruposRepository.getDependenciasToGrupos(
            request.headers["authorization"], request.body.grupoId)
            .then(function(result) {
                response.json({ StatusCode: 200, Data: result });
        }, function(getDependenciesError) {
                response.json({ StatusCode: 500, ErrorMessage: getDependenciesError });
        });
   });
});

After 8 or 9 navigations, the app stopped to work suddenly. Can you guys help me? Thank you!

What happens if "getDependenciasToGrupos" promise result is an error, you don't have "catch".

So if it is in error, there is no http response from the server (nodejs), and the browser, after waiting for responses for few requests, shuts down the connexion.

Try to add a catch handler to the promise :

gruposRepository.getDependenciasToGrupos(
            request.headers["authorization"], request.body.grupoId)
            .then(function(result) {
                response.json({ StatusCode: 200, Data: result })

            .catch(function(error){response.json({ StatusCode: 503, Data: error})});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM