title: How to solve CORS error in nodejs while using Postman?
body: <blockquote>
  <p>I created a REST Api using nodejs and mongodb and i wanted to test it in postman but while doing so I am getting a CORS error.</p>
  </blockquote>
  <pre><code>var express = require('express');
  var log = require('morgan')('dev');
  var bodyParser = require('body-parser');
  
  var properties = require('./config/properties');
  var db = require('./config/database.js');
  //hero routes
  var herosRoutes = require('./api/heros/heros.routes');
  var app = express();
  
  //configure bodyparser
  var bodyParserJSON = bodyParser.json();
  var bodyParserURLEncoded = bodyParser.urlencoded({extended:true});
  
  //initialise express router
  var router = express.Router();
  
  // call the database connectivity function
  db.mongoc();
  
  // configure app.use()
  app.use(log);
  app.use(bodyParserJSON);
  app.use(bodyParserURLEncoded);
  
  // Error handling
  app.use(function(req, res, next) {
      res.setHeader(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);
       res.setHeader(&quot;Access-Control-Allow-Credentials&quot;, &quot;true&quot;);
       res.setHeader(&quot;Access-Control-Allow-Methods&quot;, &quot;GET,HEAD,OPTIONS,POST,PUT&quot;);
       res.setHeader(&quot;Access-Control-Allow-Headers&quot;, &quot;Access-Control-Allow-Origin,Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers,Authorization&quot;);
     next();
   });
  
  // use express router
  app.use('/api',router);
  //call heros routing
  herosRoutes.hero(router);
  
  // intialise server
  app.listen(properties.PORT, (req, res) =&gt; {
      console.log(`Server is running on ${properties.PORT} port.`);
  })
  </code></pre>
  <blockquote>
  <p>Whenever i make any create or get request, i get this CORS error in postman. How to solve this?</p>
  </blockquote>
  <p>CORS Error: The request has been blocked because of the CORS policy</p>