使用401 MEAN堆栈res.redirect

时间:2018-07-19 16:53:42

标签: angular typescript express routing

当您尝试未登录而访问该页面时,我试图制作一个重定向到登录页面的MEAN应用程序。我正在使用MEAN堆栈,并试图显示将要登录的Angular页面。 / p>

我的快递路线如下:

app.use('/app/', helpers.checkLogin);

// Angular app
app.use('/app/', express.static('lib/dist'));
app.use('/app/', (req, res) => {
    res.sendFile(path.join(__dirname, '../../dist/index.html'));
});

并且有这样的角度路由设置:

const routes: Routes = [
    { path: 'login', component: LoginComponent }...]

helpers.checkLogin函数如下所示:

export function checkLogin(req: Request, res: Response, next: NextFunction)
{
    if (req.originalUrl === '/app/login') {
        next();
    } else {
        if (checkIfLoggedIn()) {
            next();
        } else {
            res.redirect('/app/login');
        }
    }
}

我的主要问题是它将不断重定向。另外,对我来说,发送带有重定向的401状态码会更好,但是res.redirect不允许400码。

谢谢

编辑1:为清楚起见,我不知道如何使用401状态代码进行重定向,并且我不了解如何设置路由,以便我仍将使用角度组件进行登录,但不进行重定向当我尝试访问此文件但尚未登录时。

0 个答案:

没有答案
相关问题