roger
roger

Reputation: 9893

how to customize cookie in express?

I am using express-session in my project:

app.use(session({
  name: "server-session-cookie-id",
  secret: 'express secret',
  maxAge: 1000 * 3600 * 24,
  saveUninitialized: true,
  resave: true,
  store: new RedisStore({client: redisClient, ttl: 260, db: 2}),
}))

express-session will generate a cookie named server-session-cookie-id when I first visit my application.

But, in my situation I want to append some information in cookie, something like this:

cookie['server-session-cookie-id'] + uuid

how can I change the cookie when the cookie first time generate?


EDIT

I think I ask the wrong question, what I want is when first visit my application(any page), I just create a cookie named uuid.

Upvotes: 0

Views: 1465

Answers (2)

user4233921
user4233921

Reputation:

You can set a cookie using res.cookie and node-uuid.

npm install node-uuid --save

var uuid = require('uuid');

Then in your middleware of your main application just check for a user object:

app.use(function(req, res, next) {
    if (!req.user) {
        res.cookie('uuid', uuid.v4());
    }
    next();
}

Assuming you have a user object on the req request object in the case of using express-session with passport.

Upvotes: 2

jfriend00
jfriend00

Reputation: 707258

If you really just want to set a new cookie with your uuid on it (as you said in your comments), then you can just do this in Express in response to any incoming request:

res.cookie('uuid', uuid);

Upvotes: 0

Related Questions