使用PM2的Production Server有时无法在routeChangeSuccess内部运行操作

时间:2018-09-26 07:28:33

标签: angularjs node.js express pm2

我当前正在使用Angular 1.3.8和ExpressJS 4.9.8,并且发现在生产服务器中,有时它没有运行我在AngularJS控制器的routeChangeSuccess中定义的任何操作。我使用PM2集群模式运行了NodeJS应用程序。因此,现在,我有流程1-4。所有进程都以NODE_ENV = production运行。

例如,这是页面加载时将要运行的代码之一:

app.controller('ViewController', ['$scope', '$http', '$route', '$location', '$sce', '$window', '$q', '$modal', '$interval', '$upload', 'ActivityService', function($scope, $http, $route, $location, $sce, $window, $q, $modal, $interval, $upload, ActivityService) {

    $scope.activity = {};
    $scope.organization = {};
    $scope.user = {};
    $scope.questions = [];

    $scope.application = {
        app: []
    };

    $scope.stars = [];

    $scope.spinnerloading = false;

    $scope.isRequireCv = false;
    $scope.isCVUploaded = false;

    /* Main Functions */

    $scope.$on('$routeChangeSuccess', function(event, routeData) {
        // Pop Up on Activities Search Page
        $scope.popupDontGo();

        _.each(ActivityService.getActivityTypes(), function(type) {
            $scope.TYPES[type.value] = type.name;
        });

        /*Use promise for multiple call API*/
        $q.all([$http.get('/api/activity/' + $route.current.params.activity_id)
                .then(function(data) {
                    /*Activity*/
                    indorelawan.verbose && console.log(data.data);

                    $scope.organization = data.data.result.organization;
                    $scope.activity = data.data.result;
                    $scope.description = $sce.trustAsHtml(data.data.result.description);
                    $scope.isRequireCv = data.data.result.isRequireCv;

                    var ratings = $scope.calculateRating();
                    var share_current_url = window.location.pathname;

                    for (var i = 0; i < ratings; i++) {
                        $scope.stars.push("fa fa-star fg-yellow");
                    }
                    for (var i = 0; i < 5 - ratings; i++) {
                        $scope.stars.push("fa fa-star");
                    }

                    return data;
                }),
                $http.get('/api/user/current')
                .then(function(data) {
                    /*User*/
                    $scope.user = data.data.result;

                    if (typeof $scope.user.cv == 'undefined' || $scope.user.cv == null) {
                        $scope.isCVUploaded = false;
                    } else {
                        $scope.isCVUploaded = true;
                    }

                    return data;
                })
        ]).then(function(data) {
            /*Check user*/
            if (data[1].data.result.role == 'manager') {
                if (url['ref'] != null && $scope.user.organization._id == $scope.activity.organization._id) $('#notificationPopup').modal('show');
            } else if (data[1].data.result.role == 'user') {
                var found = _.find(data[1].data.result.volunteers, function(volunteer) {
                    return volunteer.activity == $scope.activity._id
                });
                if (url['ref'] == 'daftar' && found==undefined) $('#previewModal').modal('show');
            }
        });

        $http
        .get('/api/activity/' + $route.current.params.activity_id + '/volunteer-in-process')
        .success(function(data) {
            $scope.volunteers = data.results;
            $scope.count = data.count;
            $scope.hasNext = data.has_next;
            $scope.next = data.next;
        });

        // TODO: Convert using service.
        $http
        .get('/api/user/current/volunteer')
        .success(function(data) {
            $scope.user.volunteers = data.results;
        });

        //Call All Questions
        $http
        .get('/api/activity/' + $route.current.params.activity_id +'/questions')
        .success(function(data) {
            $scope.questions = data.results;
        });

    });
    ...
}]);

根据代码,当页面加载时,将调用5个API,即:

  • / api / activity / activity_id /
  • / api / user / current /
  • / api / activity / activity_id /正在处理的志愿者
  • / api / user / current / volunteer
  • / api / activity / activity_id / questions

现在,在本地主机中运行此命令时,它可以完美运行。但是,在生产服务器中,有时页面未加载API。这是日志比较的示例:

Normal:
GET /activity/activity_id 200 --
GET /api/activity/activity_id/ 200 --
GET /api/user/current/ 200 --
GET /api/activity/activity_id/volunteer-in-process 200 --
GET /api/user/current/volunteer 200 --
GET /api/activity/activity_id/questions 200 --
GET / 200 --
GET /activity/search 200 --

Error:
GET /activity/activity_id 200 --
GET / 200 --
GET /activity/search 200 --

就像他们跳过了routeChangeSuccess部分一样,导致我们永远不会停止在Angular中加载,从而导致ng-cloak永不消失。我该怎么做才能避免这种情况?因为这会使网站看起来很慢。

0 个答案:

没有答案