js&&node set_cookie、get_cookie

js:

    function set_cookie(key, val,now){
        var exdate = new Date(now);
        exdate.setDate(exdate.getDate() + 30);
        exdate = exdate.toGMTString();
        document.cookie = key + '=' + val + ";path=/;domain=;expires=" + exdate;
    }

    function get_cookie(key){
        var cookies = document.cookie.split(';'), val;
        for (var i = 0; i < cookies.length; i++) {
            if (cookies[i].indexOf(key) !== -1) {
                val = cookies[i].split('=')[1];
                break;
            }
        }
        return val || null;
    }

函数直接调用即可

node:

    function set_cookie(key, val){
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + 30);
        exdate = exdate.toGMTString();
        return key + '=' + val + ";path=/;domain=;expires=" + exdate;
    }

    res.setHeader("Set-Cookie", [set_cookie()]);

  

posted @ 2014-04-25 15:08  hitbs228  阅读(461)  评论(0编辑  收藏  举报