Enabling Cross Origin Resource sharing (CORS) in ExpressJS framework on NodeJS developed using Swagger
<p>I've hosted a nodejs server on localhost generated using Swagger and I'm trying to make API calls to it using Swagger UI again hosted on localhost. The network profile in chrome is showing connection refused everytime I'm making a call because CORS header are not added to the request.</p>

<p>This is my app.js file which was generated using Swagger:</p>

<pre><code>'use strict';

var SwaggerExpress = require('swagger-express-mw');

var app = require('express')();
module.exports = app; // for testing

var config = {
  appRoot: __dirname // required config
};


SwaggerExpress.create(config, function(err, swaggerExpress) {
    if (err) { throw err; }

  // install middleware

  swaggerExpress.register(app);
  var port = process.env.PORT || 10010;
  app.listen(port);

  if (swaggerExpress.runner.swagger.paths['/hello']) {
    console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott');
  }
});
</code></pre>

<p>Till now I've tried the following methods and none of them has worked:</p>

<ol>
<li><p>Importing cors module from npm:</p>

<p>npm install cors</p></li>
</ol>

<blockquote>
  <p>In app.js</p>

<pre><code>var cors = require('cors');

app.all(cors())
</code></pre>
</blockquote>

<ol start="2">
<li>Adding CORS using statements given in enable-cors website:
<a href="http://enable-cors.org/server_expressjs.html" rel="nofollow">http://enable-cors.org/server_expressjs.html</a></li>
</ol>

<p>Both the above methods have failed for me, what can I try next? I can share more code samples if required.</p>