title: How to enable Access-Control-Allow-Origin for angular 5/nodejs?
body: <p>Read many ways for including of 'Access-Control-Allow-Origin' and none worked for me.</p>
  
  <p>I use @angular/common/http module and external url as data source.
  by the attempt to get data instead, get error:
  /////.................</p>
  
  <pre><code>Failed to load http://accounts.......com/accounts: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 503.
  </code></pre>
  
  <p><br>
  <strong>account.service.ts:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import { Injectable                    } from '@angular/core';
  import { Router                        } from '@angular/router';
  import { HttpClient, HttpParams        } from '@angular/common/http';
  import { HttpHeaders                   } from '@angular/common/http';
  
  import { Observable                    } from 'rxjs';
  import { catchError                    } from 'rxjs/operators';
  
  import { Account                       } from '../models/account';
  
  const baseUrl     : string = 'http://accounts..................com/';
  const httpOptions : any    = {
    headers: new HttpHeaders({
      //'Content-Type':  'application/json',
      'Access-Control-Allow-Headers': 'Content-Type',
      'Access-Control-Allow-Methods': 'GET',
      'Access-Control-Allow-Origin': '*'
    })
  };
  
  @Injectable()
  export class AccountService {
    private isUserLoggedIn;
    private usreName;
    private account : Account;
  
    constructor(
      private http: HttpClient,
      private router: Router
    ) {}
  
    logIn (credentials: any): Observable&lt;Account&gt; {
      return this.http.get&lt;Account&gt;(baseUrl + 'accounts');
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>app.module.ts</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import { BrowserModule                 } from '@angular/platform-browser';
  import { HttpClientModule              } from '@angular/common/http';
  import { NgModule                      } from '@angular/core';
  
  import { routing                       } from './routing';
  import { AppComponent                  } from './app.component';
  import { AppGlobal                     } from './app.global';
  
  import { AccountComponent              } from './components/account/account.component';
  
  @NgModule({
    declarations  : [
      AppComponent,
      AccountComponent,
      ....
    ],
    imports       : [
      routing,
      BrowserModule,
      HttpClientModule,
      CookieModule.forRoot()
    ],
    providers     : [AccountService, AppGlobal],
    bootstrap     : [AppComponent]
  })
  export class AppModule { }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Help please</p>
  
  <p><strong>////////////////Tried fix 1</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>//......
  import { HttpHeaders} from '@angular/common/http';
  //.......
  logIn (credentials: any): Observable&lt;Account&gt; {
  
      const headers = new HttpHeaders()
        .append('Content-Type', 'application/json')
        .append('Access-Control-Allow-Headers', 'Content-Type')
        .append('Access-Control-Allow-Methods', 'GET')
        .append('Access-Control-Allow-Origin', '*');
      return this.http.get&lt;Account&gt;(baseUrl + 'accounts',  {headers});
  }</code></pre>
  </div>
  </div>
  
  <a href="https://i.stack.imgur.com/i0Iko.png" rel="noreferrer"><img src="https://i.stack.imgur.com/i0Iko.png" alt="enter image description here"></a></p>
  
  <p>I am still getting that error :</p>
  
  <blockquote>
    <p>Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="http://localhost:4200" rel="noreferrer">http://localhost:4200</a>' is therefore not allowed access. The response had HTTP status code 503.</p>
  </blockquote>
  
  <p><strong>////////////////Tried fix 2</strong></p>
  
  <p>proxy.conf.json:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>{
    "/api": {
      "target": "http://localhost:4200",
      "secure": false,
      "pathRewrite": {
        "^/api": ""
      },
      "changeOrigin": true,
      "logLevel": "debug"
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>ng serve --proxy-config proxy.conf.json</p>
  
  <p>also error</p>