Sunday, 30 April 2017

route angular otherwise redirectTo and middleware Express node

I'm trying to use an express middleware with routeNg: the client-side routes are the following:

$routeProvider 
                            .when("/labs", {
                                    templateUrl: "views/labs.html" ,
                                    controller: "labsCtrl as ctrl"
                            }) 
                            .when("/lab/:action/:repo/:namelab", {
                                    templateUrl: "views/lab.html" ,
                                    controller: "labCtrl as ctrl"
                            }) 
                           .... 

                            .otherwise({redirectTo: "/labs"}) 

                            $locationProvider.html5Mode(true)   

While the Express routes:

app.use(express.static(__dirname + "/public"));                                                          

// Parse application/x-www-form-urlencoded & JSON                                                        
app.use(bodyParser.urlencoded({ extended: false }))                                                      
app.use(bodyParser.json())                                                                               
app.use(methodOverride('X-HTTP-Method-Override'))                                                        
app.use(function (err, req, res, next) {                                                                 
    res.status(500);                                                                                 
    res.end(err + "\n");                                                                             
})                                                                                                       

app.use(function (req, res, next)  {                                                                     
    console.log("SONO QUI")                                                                          
    Checker.isInstalled(function(err, installed) {                                                   
            if(err) {                                                                                
                    console.log("SOME ERROR") ;                                                      
            }                                                                                        
            else if(installed) {                                                                     
                    next()                                                                           
            }                                                                                        
              else {                                                                                 
                    res.redirect('/installation.html')                                               
                    }                                                                                
            });                                                                                      
});                                                                                                      

// JSON API                                                                                              
//Installation                                                                                           
app.post('/dsp_v1/installation/', installationHandler.installation)                                      



//Api labels                                                                                             
app.get('/dsp_v1/labels/:repo', labels.allLabels)     
... 

The server listens on localhost:8080 .

When the isInstalled condition is false I've to redirect at the installation.html page. This works correctly if I try to digit any url on the browser, but when I simply digit "localhost:8080" without no url it won't go to the installation.html but at the default condition of ng-route ( .otherwise({redirectTo: "/labs"})) even if the console.log("SONO QUI") testing statement is called . Any advice ? Thanks in advance



via Giper

No comments:

Post a Comment