Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
node.js express request Proxy Response Cookies
// where res is the Express res
// where response is the request module response
// assumes Lodash or Underscore is present
function proxyResponseCookies (res, response) {
var setCookie = response.headers['set-cookie'];
if (setCookie) {
_.each(setCookie, function (cookie) {
var name;
var value;
var cookieOptions = {};
cookie = cookie.split(';');
_.each(cookie, function (pairs, index) {
var pair = pairs.split('=');
var pairKey = (pair[0] || '').trim();
var pairValue = (pair[1] || '').trim();
// option
if (index) {
// expires
if (pairKey === 'expires') {
cookieOptions[pairKey] = new Date(pairValue);
}
// other
else {
cookieOptions[pairKey] = pairValue;
}
}
// name/value
else {
name = pair[0];
value = pair[1] || '';
}
});
res.cookie(name, value, cookieOptions);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment