title: How pass cookie throw node server to REST API in Angular Universal?
body: <p>I have an Angular app with server side rendering. I wont to execute one rest request only on server side, so I place such code in if (isPlatformServer(this.platformId)) block. </p>
  
  <p><strong>app.component.ts</strong></p>
  
  <pre><code>import {Component, Inject, OnInit, PLATFORM_ID} from "@angular/core";
  import {HttpClient, HttpHeaders} from "@angular/common/http";
  import "rxjs/add/operator/map";
  import {Http} from "@angular/http";
  import {isPlatformBrowser, isPlatformServer} from "@angular/common";
  
  @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
  })
  export class AppComponent implements OnInit{
    constructor(private http: HttpClient, @Inject(PLATFORM_ID) private platformId: Object){ }
  
    ngOnInit(): void {
      if (isPlatformBrowser(this.platformId)) {
        /* Client only code. */
      }
  
      if (isPlatformServer(this.platformId)) {
        /* Server only code. */
        this.http.get('http://localhost:8080/tracking', { withCredentials: true }).subscribe(data =&gt; {
          console.log("Init tracking")
        });
      }
    }
  }
  </code></pre>
  
  <p>Everyting ok, this http.get called only in server side, but httprequest on my API comes without cookies. So, in my node server I add cookie throw cookie-parser: </p>
  
  <pre><code>let cookieParser = require('cookie-parser');
  app.use(cookieParser()); 
  </code></pre>
  
  <p>And I saw them in console.log(req.cookies), but they still don't come to the API.</p>
  
  <p><strong>server.ts</strong></p>
  
  <pre><code>import "zone.js/dist/zone-node";
  import "reflect-metadata";
  import {enableProdMode} from "@angular/core";
  import * as express from "express";
  import {join} from "path";
  import {readFileSync} from "fs";
  // Express Engine
  import {ngExpressEngine} from "@nguniversal/express-engine";
  // Import module map for lazy loading
  import {provideModuleMap} from "@nguniversal/module-map-ngfactory-loader";
  import {CookieService} from "ngx-cookie-service";
  
  // Faster server renders w/ Prod mode (dev mode never needed)
  enableProdMode();
  
  // Express server
  const app = express();
  let cookieParser = require('cookie-parser');
  app.use(cookieParser());
  
  const PORT = process.env.PORT || 4200;
  const DIST_FOLDER = join(process.cwd(), 'dist');
  
  // Our index.html we'll use as our template
  const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString();
  
  // * NOTE :: leave this as require() since this file is built Dynamically from webpack
  const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle');
  
  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  app.engine('html', ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [
      CookieService,
      provideModuleMap(LAZY_MODULE_MAP)
    ]
  }));
  
  app.set('view engine', 'html');
  app.set('views', join(DIST_FOLDER, 'browser'));
  
  /* - Example Express Rest API endpoints -
    app.get('/api/**', (req, res) =&gt; { });
  */
  
  // Server static files from /browser
  app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), {
    maxAge: '1y'
  }));
  
  // ALl regular routes use the Universal engine
  app.get('*', (req, res) =&gt; {
    console.log('Cookies req: ', req.cookies);
    res.render('index', { req });
  
  });
  
  // Start up the Node server
  app.listen(PORT, () =&gt; {
    console.log(`Node Express server listening on http://localhost:${PORT}`);
  });
  </code></pre>
  
  <p>How can I pass cookie throw node &amp; get them on my rest end point in API?</p>