【问题标题】:nodejs read session from databasenodejs从数据库读取会话
【发布时间】:2011-08-27 14:09:44
【问题描述】:

我正在使用 nodejs、nowjs、codeigniter 和 mysql 编写应用程序。我使用数据库来存储会话。有没有一种好方法可以检查和检索 nodejs 中的会话 cookie?因此,例如,如果我需要使用 nowjs 检查用户是否拥有数据库中即将被删除的特定行。例如:

服务器:

//some code here to get session cookie and retrieve stored data from database

var session = { user_id: //userid from database }

everyone.now.deleteGuestbook = function(recordId) {
    var owner_id = client.query(
        'SELECT owner_id FROM guestbook WHERE id = ?',
        [recordId]
    );
    if (session.user_id === owner_id) {
        client.query(
        'DELETE FROM guestbook WHERE id = ?',
        [recordId], function() {
                 everyone.now.deleteRecord;
            }
        );
    }
}

客户:

//Delete guestbook record
$('#guestbook_records li').live('click', function(e) {
    if ($(e.target).is('a.delete')) {
        var id = $(this).find('a.delete').attr('href');
        dialogBox('warning', 'Are you sure you want to delete the record?',
            function() {
                now.deleteGuestbook(id);
            });
    }
    else {
        return;
    }
    return false;
});

now.deleteMessage = function(id) {
    $('li[class="' + id + '"]').slideUp(400, function() {
        $(this).remove();
    });
};

【问题讨论】:

  • 您是在寻找代码审查还是有问题?
  • @Raynos 那么问题是关于如何在 nodejs 中检索 cookie 数据。考虑到我编写的代码,如果有特定的方法可以将代码放在那里。
  • 乔治,我也有同样的情况。你找到解决这个问题的方法了吗?我也在寻找一种将值从会话 cookie 传递给 nodejs 的方法。那么,您是如何设法解决这个问题的呢? Rob Raisch 在下面的回复似乎对我不起作用。
  • @wenbert 看看这个:stackoverflow.com/questions/8364642/…

标签: php javascript codeigniter node.js nowjs-sockets


【解决方案1】:

会话 ID 通常保存在客户端 cookie 中,这些 cookie 在 httpServer 处理程序中显示为 request.headers.cookie。

您需要知道包含会话 ID 的 cookie 的名称,然后:

var cookies=(function(str){                     # create hash from cookie string
      var result={};
      str.split(/;\s+/).forEach(function(e){
        var parts=e.split(/=/,2);
        result[parts[0]]=parts[1]||'';
      });
      return result;
    })(request.headers.cookie),
    sessionCookieName='session',                # name of cookie holding session id
    sessionId=cookies[sessionCookieName]||'';   # session id or ''

【讨论】:

  • 您好 Rob Raisch,我收到一条错误消息,提示“未定义请求”。有任何想法吗?和OP一样,我也在用nowjs,想访问服务器端的会话。
  • Wenbert,你的处理程序是怎么调用的?通常,它有两个参数:请求和响应。无论您如何编写处理程序,我都会将示例中对“请求”的引用更改为您在处理程序函数中命名的第一个参数。
【解决方案2】:

你的客户端代码很乱

// live is bad and inefficient
$('#guestbook_records li').live('click', function(e) {
    // this kind of check seems horrid
    if ($(e.target).is('a.delete')) {
        // not very optimum to find it. 
        var id = $(this).find('a.delete').attr('href');
        dialogBox('warning', 'Are you sure you want to delete the record?',
            function() {
                now.deleteGuestbook(id);
            });
    }
    else {
        // why return here?
        return;
    }
    // return false should be in if block.
    return false;
});

试试这个

$("#guestbook_records li a.delete").click(function(e) {
    var id = this.href;
    dialogBox("warning", "Are you sure you want to delete the record?",
        function() {
             now.deleteGuesBook(id);
        });
    e.preventDefault();
}

还有.hasClass更好

now.deleteMessage = function(id) {
    $('li').hasClass(id).slideUp(400, function() {
        $(this).remove();
    });
};

【讨论】:

    猜你喜欢
    • 2017-04-21
    • 2021-08-13
    • 2015-06-29
    • 1970-01-01
    • 2013-07-04
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    相关资源
    最近更新 更多