Home > Back-end >  NodeJS express-session, if cookie path is set, cookie doesn't save
NodeJS express-session, if cookie path is set, cookie doesn't save

Time:10-27

I have a web page with passport login and express session.

Everything works but the cookie and I noticed what was the problem ( if I define the path of the cookie, the cookie doesn't add to the domain. )

If anyone knows why it happens please tell me.

(session store: connect-mongo 4.6.0)

app.use(session({  
  secret: 'secret',  
  store: Store.create({  
    mongoUrl: 'myMongoURL',  
    dbName: 'db-sessions'  
  }),  
  cookie: {  
    path: '/menu',  
    domain: 'mydomain.com',  
    maxAge: 60000 * 60 * 24,
  },  
  resave: false,  
  saveUninitialized: false,  

}))

CodePudding user response:

When you add a path to the cookie, that cookie will ONLY be sent to the server for a request to that specific path.

So, only a request for the /menu path sent to your server will include that specific cookie. A request to your server for any other path will not include that cookie. The client is only sending the cookie with requests that match that specific path.

Note that a path in the cookie of /menu will match a request to /menu and also /menu/somethingelse.

  • Related