2016-01-28 43 views
1

我不明白爲什麼從2016開始的網址不起作用。它只是永遠加載。Express.js條件router.all()

(/),管理員(/admin/*)/home工作沒有問題。

function stringStartsWith(string, prefix) { 
    return string.slice(0, prefix.length) == prefix; 
} 

router.get('/', csrfProtection, indexOnly, indexController.index); 

router.get('/admin', adminOnly, adminController.index); 

router.all('/*', function(req, res, next) { 
    if (req.originalUrl == '/home') { 
    next(); 
    } else if (stringStartsWith(req.originalUrl, "/admin")) { 
    router.all('/admin/*', function(req, res, next) { 
     if (req.originalUrl == '/admin') { 
     next(); // it doesn't do anything, just allows the route above to work (admin welcome page.) 
     } else { 
     res.sendFile(path.resolve('views/backend/index.html')); 
     } 
    }); 
    } else if (stringStartsWith(req.originalUrl, "/2016")) { 
    router.all('/2016/*', function(req, res, next) { 
     res.sendFile(path.resolve('views/frontend/index/index.html')); 
    }); 
    } else { 
    res.sendFile(path.resolve('views/frontend/index.html')); 
    } 
}); 
+1

你應該從來沒有** **添加路由內另一條路線,這是沒有意義的,整個狀態是沒有意義的,應予刪除。 – adeneo

+0

我的不好,謝謝。我把他們分開。 – salep

回答

1

爲什麼你把2016路線放在其他路線內?它應該僅僅是像其他人另一條路線:

function stringStartsWith(string, prefix) { 
    return string.slice(0, prefix.length) == prefix; 
} 

router.get('/', csrfProtection, indexOnly, indexController.index); 

router.get('/admin', adminOnly, adminController.index); 

router.all('/2016/*', function(req, res, next) { 
    res.sendFile(path.resolve('views/frontend/index/index.html')); 
}); 

router.all('/*', function(req, res, next) { 
    if (req.originalUrl == '/home') { 
    next(); 
    } else if (stringStartsWith(req.originalUrl, "/admin")) { 
    router.all('/admin/*', function(req, res, next) { 
     if (req.originalUrl == '/admin') { 
     next(); // it doesn't do anything, just allows the route above to work (admin welcome page.) 
     } else { 
     res.sendFile(path.resolve('views/backend/index.html')); 
     } 
    }); 
    } else { 
    res.sendFile(path.resolve('views/frontend/index.html')); 
    } 
});