Answer the question
In order to leave comments, you need to log in
How to pull out the right cookie?
Hello. There is a code, yesterday a man threw
var get_cookies = function(request, cookieName) {
var cookies = {};
request.headers && request.headers.cookie.split(';').forEach(function(cookie) {
var parts = cookie.match(/(.*?)=(.*)$/)
cookies[ parts[1].trim() ] = (parts[2] || '').trim();
});
return cookies[cookieName];
};
Answer the question
In order to leave comments, you need to log in
You can also connect plugins.jquery.com/cookie
After that, you can simply work with cookies:
// this is how you can set new cookies or overwrite the values of existing ones:
// you can get the value of existing cookies like this:
// if the requested cookies do not exist , then this function will return null
// otherwise you can delete cookies$.cookie('cookie_name', null);
googled a simple lib https://github.com/defunctzombie/node-cookie
you can not use it, but just look how it parses data from the header
. Express frameworks already have built-in work with cookies
var getCookie = function (name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question