How i can get cookie value by cookie name?
res.headers['set-cookie']
returns all cookies.
i need like res.headers['set-cookie']['cookieName']
We can check cookies by visiting localhost:3000/setcookie. This will show a message as cookies are added. We can check the cookies by visiting localhost:3000/getcookie. Now we can see that our cookies are added to the cookies object as shown above.
get() The get() method of the cookies API retrieves information about a single cookie, given its name and URL. If more than one cookie with the same name exists for a given URL, the one with the longest path will be returned.
Now to use cookies with Express, we will require the cookie-parser. cookie-parser is a middleware which parses cookies attached to the client request object. To use it, we will require it in our index. js file; this can be used the same way as we use other middleware.
I have found this solution may work for you
var get_cookies = function(request) {
var cookies = {};
request.headers && request.headers.cookie.split(';').forEach(function(cookie) {
var parts = cookie.match(/(.*?)=(.*)$/)
cookies[ parts[1].trim() ] = (parts[2] || '').trim();
});
return cookies;
};
and then you can use
get_cookies(request)['cookieName']
But if you are using express.I will suggest you
var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
app.use(cookieParser());
then to get cookie value.You can
req.cookies['cookieName']
Hope this help. I found these solutions and worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With