B
B
Burgul2014-03-11 08:20:04
Node.js
Burgul, 2014-03-11 08:20:04

Node JS + Socket.io How to get specific cookie value?

I'm trying to get a specific cookie value for authorization.

io.configure(function (){
    io.set('authorization', function (handshakeData, callback) {
        var cook = handshakeData.headers.cookie;
        console.log(cook);
        callback(null, true); // error first callback style
    });
});

I get either an array or a string. When using .indexOf() and .split() the server does not come up.
In the first case, it says: TypeError: Cannot call method 'indexOf' of undefined
In the second: TypeError: Cannot call method 'split' of undefined
I need to get just one cookie record to check, not the whole array.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Meketyuk, 2014-03-11
@Burgul

Maybe this will help you.
The fact is that there can be several cookies in the header, so you get a string or an array.
In order to get a specific cookie, you need to parse the answer and then take the right one. The link above has it all.
Something like this...

io.configure(function (){
    io.set('authorization', function (handshakeData, callback) {
        var cookies = parseCookies(handshakeData);
        console.log(cookies);
        callback(null, true); // error first callback style
    });
});

function parseCookies(request) {
    var list = {},
        rc = request.headers.cookie;

    rc && rc.split(';').forEach(function(cookie) {
        var parts = cookie.split('=');
        list[parts.shift().trim()] = unescape(parts.join('='));
    });

    return list;
}

I
Ilya, 2015-06-21
@dagtec

It's not a font.
Life hack: if you need vector logos of some factories or combines and you can't find them on sites with vectors, then look at their site in sections like "press about us" or their PDF editions.
In 80% of cases you will find a vector logo in its normal form. As a rule, even requests to the press service "send a vector, at least corel" do not give a result.
In this case, this is the symbolism of the former RAO UES. Search there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question