服务器上的路由会扰乱客户端的路由?

时间:2021-12-24 23:55:30

Using passport.js I don't need any client side javascript until the user is logged in.

使用passport.js我不需要任何客户端javascript,直到用户登录。

On my server side, I have a router that handles some login logic. So when the user successfully logs in, I route the user to the /profile route.

在我的服务器端,我有一个处理一些登录逻辑的路由器。因此,当用户成功登录时,我将用户路由到/ profile路由。

This kind of confuses me, since express is handling the overall framework. The /profile route renders my profile template res.render('profile', { user : req.user }); and nicely passes in the server model for the user into the template, cool stuff!

这种混淆我,因为快递处理整体框架。 / profile route渲染我的个人资料模板res.render('profile',{user:req.user});并很好地将用户的服务器模型传递到模板中,很酷的东西!

But this is what gets me, if my app is now on /profile a path that doesn't exist in my directory and I define my directory here app.use('/', express.static(path.join(__dirname, 'public'))); this results in bad architecture.

但是这就是我的,如果我的应用程序现在在/配置文件中我的目录中不存在的路径,我在这里定义我的目录app.use('/',express.static(path.join(__ dirname,')上市')));这导致糟糕的架构。

Because at times I get 404 errors on my js css. I have to route like this /routes#chat on my client side backbone router.

因为有时我的js css会出现404错误。我必须在我的客户端骨干路由器上这样路由/ routes#chat。

My Question

So would it be a good idea to somehow after user success to redirect back to localhost:3000 and now destroy the login view and render a user profile view? Could someone kind of give me an insight how I could route like that, I have a general idea but not so sure how to do that when I think the server side would need to communicate with the client side, or maybe not?

因此,在用户成功重定向回localhost:3000并以后销毁登录视图并呈现用户配置文件视图之后,以某种方式这是一个好主意吗?有人可以给我一个洞察力,我可以这样做,我有一个大致的想法但不太确定如何做到这一点当我认为服务器端需要与客户端沟通,或者可能不是?

If the above is a bad idea, can someone give me advice on how to better look at this?

如果以上是一个坏主意,有人可以给我建议如何更好地看待这个?

Edit: Routes

app.get('/', function(req, res) {
    res.render('index'); 
});

app.get('/login', function(req, res) {
    res.render('login', { message: req.flash('loginMessage') }); 
});

app.get('/profile', isLoggedIn, function(req, res) {
    res.render('profile', { user : req.user });
    res.redirect('/');
    io.sockets.on('connection', function (socket) {
        socket.userName = req.user.local.email;
        users[socket.userName] = socket;
        console.log(Object.keys(users))
    });
});

1 个解决方案

#1


1  

use session to remember the login or use some custom function to query the status of login.

使用session来记住登录或使用一些自定义函数来查询登录状态。

app.get('/', function(req, res) {

    if (req.session.isLogin) {
        res.render('profile', { user : req.user });
        // put other code here
    }
    else
        res.redirect('/login'); 
});

app.get('/login', function(req, res) {
    res.render('login', { message: req.flash('loginMessage') }); 
});

#1


1  

use session to remember the login or use some custom function to query the status of login.

使用session来记住登录或使用一些自定义函数来查询登录状态。

app.get('/', function(req, res) {

    if (req.session.isLogin) {
        res.render('profile', { user : req.user });
        // put other code here
    }
    else
        res.redirect('/login'); 
});

app.get('/login', function(req, res) {
    res.render('login', { message: req.flash('loginMessage') }); 
});