title: Angular 7 app getting CORS error from angular client
body: <p>I have developed an angular 7 app with express backend. Express running on <code>localhost:3000</code> and angular client is running on localhost:4200.</p>
  
  <p>In the <code>server.js</code> I have (not the entire code)</p>
  
  <pre><code>const app = express();
  // Enable CORS
  app.use(cors());
  // Get our API routes
  const api = require('./api');
  // Set our api routes
  app.use('/api', api);
  app.use(express.static(__dirname + '/dist/sfdc-event'));
  </code></pre>
  
  <p>In the api.js file, I have router.get(‘/oauth2/login’) which redirects to <a href="https://example.com" rel="noreferrer">https://example.com</a> which sends an access token and authenticates the user (OAuth2 authentication).</p>
  
  <p>When I am calling the url <a href="http://localhost:3000/api/oauth2/login" rel="noreferrer">http://localhost:3000/api/oauth2/login</a> everything is working fine, but when I am trying to do the same from angular component.ts -> service.ts I am getting the following error.</p>
  
  <blockquote>
    <p>Cross-Origin Request Blocked: The Same Origin Policy disallows reading
    the remote resource Reason: CORS header ‘Access-Control-Allow-Origin’
    missing</p>
  </blockquote>
  
  <p>Angular app flow as follows login.component.ts which has a button calling a service api.service.ts which executes a http get.</p>
  
  <pre><code>login.component.ts
  
  sfdcLogin(): void {
    console.log('DEBUG: LoginComponent: ', 'Login button clicked..');
   this.apiService.login().subscribe( data =&gt; { console.log( data ); });
  }
  </code></pre>
  
  <p>api.service.ts</p>
  
  <pre><code>login() {
    console.log('DEBUG: APiService login(): ', 'login() function.');
    const URL = 'oauth2/login';
    console.log('DEBUG: ApiService login URL : ', `${environment.baseUrl}/${URL}`.toString());
    return this.http.get(`${environment.baseUrl}/${URL}`)
      .pipe( map( res =&gt; res ));
  }
  </code></pre>
  
  <p>Can someone help me get past the error? I have a) CORS b) serving static files from server.js as </p>
  
  <pre><code>app.use(express.static(__dirname + '/dist/sfdc-event'));
  </code></pre>
  
  <p>c) dynamic environment variable.
  What else I am missing?</p>