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

315
Views
Using max-age in cookies nodejs using res.setheader

I am testing nodejs..

I am using Express.

I want to make a cookie.

the way I know is-

const express =  require("express");
var app =  express();

app.get("/set-cookie",(req,res)=>{
   res.setHeader("set-cookie", "value=true");
});

I want to make a cookie using res.setHeader() with the max-age set to 1 minute without adding more dependencies

I have tried

const express =  require("express");
var app =  express();

app.get("/set-cookie",(req,res)=>{
   res.setHeader("set-cookie", "value=true;MaxAge="1000*60");
});

But it is not setting the max-age to one minute.

What should I do?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use res.cookie method to set the maxAge, below code will return a cookie which expires in 1 minute:

app.get("/set-cookie",(req,res)=>{
    res.cookie('cookieName', "value", { maxAge: 60*1000 });
    res.send();
});

If you're using plain HTTP header (setHeader) you must keep in mind that:

Max-Age= Optional Number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. If both Expires and Max-Age are set, Max-Age has precedence.

so your code should look like this:

app.get("/set-cookie",(req,res)=>{
    res.setHeader("Set-Cookie", "key=value; Max-Age=60");
    res.send();
});
over 4 years ago · Santiago Trujillo 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!