title: Nodejs express, Heroku CORS
body: <p>The server side of my app was built using Node with express,</p>
  
  <p>It was working fine locally, but now I've uploaded to Heroku</p>
  
  <p>I'm getting CORS errors, even though I've handled it within the app</p>
  
  <p>index.js</p>
  
  <pre><code>var express = require('express');
  var bodyParser = require('body-parser');
  
  var http = require('http');
  var path = require('path');
  var twit = require('twitter');
  var app = express();
  var port = process.env.PORT || 3000;
  
  var twitter = new twit({
    consumer_key:  'myKey',
    consumer_secret: 'mySecret',
    access_token_key: 'myKey',
    access_token_secret: 'mySecret'
  });
  app.set('port', (port));
  var server = http.createServer(app).listen(port, function() {
    console.log('Server listening on port ' + port);
  });
  app.use(bodyParser.urlencoded({extended: true}));
  
  app.use(function (req, res, next) {
      res.setHeader('Access-Control-Allow-Origin', '*');
      res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
      res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS');
      next();
  });
  
  app.get('/gettweets', function (req, res) {
    twitter.get('statuses/user_timeline', { id: 23445555, count: 3 }, function(err, tweets, response) {
        if (!err) {
           res.json(tweets);
        }
        else {
          return res.status(500).json({
            title: 'An error has occured',
            error: err
          })
        }
      })
  })
  </code></pre>
  
  <p>And in my http call</p>
  
  <pre><code>export class SocialService {
  
    private url = 'https://myserver.herokuapp.com/';
    constructor(private http: Http) { }
  
    initialise(){
      var headers = new Headers();
          headers.append('Content-Type', 'application/json');
  
      return this.http.get(this.url+'gettweets', {headers: headers})
          .map((response: Response) =&gt; {
          console.log(response.json());
            return response.json();
          })
          .catch((error: Response) =&gt;  Observable.throw(error.json()) )
  
    }
  </code></pre>
  
  <p>}</p>
  
  <p>And the build log from Heroku</p>
  
  <pre><code>-----&gt; Node.js app detected
  -----&gt; Creating runtime environment
  
         NPM_CONFIG_LOGLEVEL=error
         NPM_CONFIG_PRODUCTION=true
         NODE_VERBOSE=false
         NODE_ENV=production
         NODE_MODULES_CACHE=true
  -----&gt; Installing binaries
         engines.node (package.json):  unspecified
         engines.npm (package.json):   unspecified (use default)
  
         Resolving node version 6.x via semver.io...
         Downloading and installing node 6.10.3...
         Using default npm version: 3.10.10
  -----&gt; Restoring cache
         Loading 2 from cacheDirectories (default):
         - node_modules
         - bower_components (not cached - skipping)
  -----&gt; Building dependencies
         Installing node modules (package.json)
  -----&gt; Caching build
         Clearing previous node cache
         Saving 2 cacheDirectories (default):
         - node_modules
         - bower_components (nothing to cache)
  -----&gt; Build succeeded!
  -----&gt; Discovering process types
         Procfile declares types     -&gt; (none)
         Default types for buildpack -&gt; web
  -----&gt; Compressing...
         Done: 17.8M
  -----&gt; Launching...
         Released v5
         https://myserver.herokuapp.com/ deployed to Heroku
  </code></pre>
  
  <p>And error in Browser</p>
  
  <pre><code>XMLHttpRequest cannot load https://myserver.herokuapp.com/gettweets.
  Response to preflight request doesn't pass access control check:
  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>This is the error I'm getting from Heroku, something to do with the path I'm requesting </p>
  
  <pre><code>2017-05-11T12:54:05.960151+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=OPTIONS path="/gettweets" host=maleonserver.herokuapp.com request_id=5c052790-67df-4677-be8b-14cc2bc71292 fwd="5.67.244.130" dyno= connect= service= status=503 bytes= protocol=https
  </code></pre>