Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
How to set multiple cookies with setHeader in NodeJs

I have been working on a node js project, and I need to set two different cookies. But when I set them, the second one always overwrites the first one. here is the code I am using below

                              res.setHeader('set-cookie', [
                                'x-authsession=' + cookieParsed['x-authsession']
                                 + '; Path=' + cookieParsed['Path'] + '; Expires='
                                + cookieParsed['Expires']
                                + `;  `, ]
                                );
                            


                            res.setHeader('set-cookie', [
                                'x-device=' + cookieParsed['x-device']
                                + '; Path=' + cookieParsed['Path'] + '; Expires='
                                + cookieParsed['Expires']
                                + `; `,
                            ]);

but only x-device gets stored on the browser. Anyone knows how to fix this please?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Instead of explicitly modifying the set-cookie header you can use res.cookie.

res.cookie(
    'x-authsession',
    cookieParsed['x-authsession'],
    {
        path: cookieParsed['Path'],
        expires: cookieParsed['Expires']
    }
);
res.cookie(
    'x-device',
    cookieParsed['x-device'],
    {
        path: cookieParsed['Path'],
        expires: cookieParsed['Expires']
    }
);

Also, as said here,

To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.

Side thoughts, looks like you are trying to use session cookies manually. That isn't a good idea to do. Instead checkout the express-session package on npm.

about 4 years ago · Juan Pablo Isaza Report

0

this works fine for me:

res.setHeader("Set-Cookie", 
    [
    cookie.serialize("cookie1", "data1"),
    cookie.serialize("refreshToken", refreshToken)
    ]
)
res.end()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!