1

在我的应用程序的首页,用户可以注册一个帐户然后登录。它表示为首页上的登录和注册按钮,然后在单击任何一个时显示适当的表单。

如果用户已经登录,我想用注销按钮替换这两个按钮,但我需要先通知客户。

在我的 index.js 中,我像这样提供静态 html

app.use(express.static('public'));

我以为我可以执行以下操作

app.get('/', function(req, res) {
    // inform the client if req.user isn't null
});

但从未调用回调

4

2 回答 2

0

嗯!我想会有一个登录/注册表单,所以必须有两条路线,一条有.get(),第二条有.post()

app.get('/', function(req, res) {
    // This is for navigation to the home page
}).post('/', function(req, res){
    // inform the client here about the req.body.user
});

我猜你已经设置了这个:

app.use(bodyParser.urlencoded({extended:true})); // for "formurlencoded"
app.use(bodyParser.json()); // for "application/json"

如果没有,那么您必须先加载此模块require('body-parser'),并且仅当您使用express 4时才需要它。

于 2015-04-30T14:40:13.063 回答
0

我找到了解决方案。

在我的 index.js 中,我有这个

app.get('/isloggedin', function(req, res) {
    res.json({ loggedin: (req.user != null) });
});

然后我可以发送一个获取 /isloggedin 的请求并处理结果

$.get('/isloggedin', {}, function(data) {
    if (!data.loggedin) {
        // remove logged-in only elements
    } else {
        // remove logged-out only elements
    }
});
于 2015-04-30T15:50:35.797 回答