title: Angular 7 / Express Session not persisting
body: <p>I have an Angular 7 app connecting to a Express API backend and the session doesn't seem to persist.</p>
  
  <p>In Express I have the following endpoint:</p>
  
  <pre><code>router.get('/getsession', function(req, res) {
      console.log(`Session ID: ${req.session.id}`);
      res.status(200).json({ sessionid: req.session.id });
  });
  </code></pre>
  
  <p>Here's an example of the output for two consecutive runs of <code>/getsession</code>:</p>
  
  <blockquote>
    <p>Session ID: NMi8AXhX1wf9xui0WDFwENZ_3QON_iYN</p>
    
    <p>Session ID: pNWcPTeJVlC8rKySw6ty5xSPa9sSME8x</p>
  </blockquote>
  
  <p>I've enabled the Credentials header for Express, so it will accept it:</p>
  
  <pre><code>const cors = require("cors"); 
  app.use(cors({
    credentials: true,
  }));
  </code></pre>
  
  <p>And I've also enabled <code>withCredentials</code> for <code>Angular HttpClient</code>, so that it will send the cookie along with the POST request:</p>
  
  <pre><code>API_URL: string = "http://localdev.com:4200/api";
  options = {
    headers: new HttpHeaders({
      'Content-Type' : 'application/json',
      'Cache-Control': 'no-cache',
      'Credentials': 'same-origin'
    }),
    withCredentials: true,
  }
  
  getSessionInfo() {
    return this.http.get(`${this.API_URL}/users/getsession`, { withCredentials: true })
    .pipe(
      catchError(this.handleError)
    )
  }
  </code></pre>
  
  <p>There's an Angular proxy from <code>localhost:4200</code> to <code>localhost:4044</code> so the API requests can be processed.</p>
  
  <p>Any help would be appreciated, thanks in advance :)</p>
  
  <p>EDIT: Interestingly enough, the cookie is being passed properly onto Express, however it's still creating a new session for each request. The following is the result of <code>req.session</code> when calling the <code>/getsession</code> endpoint.</p>
  
  <blockquote>
    <p>{ 'if-none-match': 'W/"b8-afvqPuftgTLN3Wn5o/ZQx8jUsv0"',</p>
    
    <p>cookie: '_ga=GA1.2.1851469997.1544357368; _gid=GA1.2.1246771476.1544357368; _gat_gtag_UA_99682244_1=1',</p>
    
    <p>'accept-language': 'en-US,en;q=0.9,bg;q=0.8,mt;q=0.7',</p>
    
    <p>'accept-encoding': 'gzip, deflate',</p>
    
    <p>referer: '<a href="http://localdev.com:4200/user/register" rel="nofollow noreferrer">http://localdev.com:4200/user/register</a>',</p>
    
    <p>'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',</p>
    
    <p>accept: 'application/json, text/plain, <em>/</em>',</p>
    
    <p>connection: 'close',</p>
    
    <p>host: 'localdev.com:4044' }</p>
    
    <p>Session ID: XWKGlJPrzYeRBU3Hi7RIAaWpowGU6Fuz</p>
    
    <p>{ 'if-none-match': 'W/"b8-mMGAHD1Tmbv1r5T+YChLkQoq988"',</p>
    
    <p>cookie: '_ga=GA1.2.1851469997.1544357368; _gid=GA1.2.1246771476.1544357368; _gat_gtag_UA_99682244_1=1',</p>
    
    <p>'accept-language': 'en-US,en;q=0.9,bg;q=0.8,mt;q=0.7',</p>
    
    <p>'accept-encoding': 'gzip, deflate',</p>
    
    <p>referer: '<a href="http://localdev.com:4200/user/register" rel="nofollow noreferrer">http://localdev.com:4200/user/register</a>',</p>
    
    <p>'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',</p>
    
    <p>accept: 'application/json, text/plain, <em>/</em>',</p>
    
    <p>connection: 'close',</p>
    
    <p>host: 'localdev.com:4044' }</p>
    
    <p>Session ID: T4SnSqGfo9lOWGpiyPQS0LLJgXsRnZ4T</p>
  </blockquote>