8

I want http://mydomain.com to be the same as http://www.mydomain.com

And all other subdomains.

I want sessions and cookies to hold!

user847495
  • 9,831
  • 17
  • 45
  • 48

1 Answers1

12

Has nothing to do with Express. It's the settings on the cookie itself that matter. Set its domain to .mydomain.com and you should be fine.

EDIT: The OP wanted more details, so here are the examples from the code.

  connect.createServer(
      connect.cookieParser()
    , connect.session({ cookie: { domain : ".mydomain.com" }})
  );

and

 res.cookie('remember', 1, { domain : ".mydomain.com" });

should work.

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
  • How do I do that? I simply use Express to handle my cookies... I do req.session.abc='123'; console.log(req.session.abc); – user847495 Aug 16 '11 at 07:03
  • Do I need the last part (res.cookie) if I already did it in createServer? – user847495 Aug 16 '11 at 08:29
  • The arguments in createSession control the cookie(s) created to support the session. If you create any cookies by hand, you have to set the domain by hand. I believe. Try it and see. – Michael Lorton Aug 16 '11 at 16:01
  • For **localhost** you **have to have two dots**, ``domain: 'localhost'`` does not work. This does work: ``domain: '.app.localhost'`` for all the subdomains of app.localhost, such as: ``api.app.localhost:3000/'`` – AmpT Feb 27 '14 at 13:31
  • see my answer here for more info on how to handle it on **localhost**: http://stackoverflow.com/a/22070413/2728686 – AmpT Feb 27 '14 at 13:49