title: How to share cookies across ports/domains with Express
body: <p>I have two node.js (express) apps running on two different ports. One is running on <code>localhost:3000</code> and the other is running at <code>localhost:4000</code>. The app on port <code>3000</code> has the following cookie configuration:</p>
  
  <pre><code>app.use(express.cookieParser())
  
  app.use(express.session({
      key: settings.session.key,
      secret: settings.session.secret,
      cookie: settings.session.cookie,
      fingerprint: function () { return '' },
      store: new MemoryStore()
  }))
  </code></pre>
  
  <p>And the other app (on port <code>4000</code>) has:</p>
  
  <pre><code>app.use(express.cookieParser())
  
  app.use(express.session({
      key: settings.session.key,
      secret: settings.session.secret,
      cookie: settings.session.cookie,
      fingerprint: function() { return ''  },
      store: new MongoSessionStore({ db: db })
  }))
  </code></pre>
  
  <p>They are both using the same session configuration object (only difference is one is being stored in MongoDB while the other is in-memory.</p>
  
  <p>I set a cookie like so on <code>localhost:3000</code>: </p>
  
  <pre><code>res.cookie('mycookie', 'bar', { domain: 'localhost:4000' })
  </code></pre>
  
  <p>And I then POST (with jquery.ajax) to a route on <code>localhost:4000</code>, and the cookie <code>mycookie</code> is not present. </p>
  
  <p>Note: I have CORS setup on <code>localhost:4000</code> to accept the origin <code>localhost:3000</code>, and when I post with Jquery I use <code>xhrFields: { withCredentials: true }</code>.</p>
  
  <p>So my question is, how to configure the apps correctly to set cookies to one another? :)</p>