title: CORS issue in express.js project
body: <p>I am trying to develop a MERN project. My code in <code>server.js</code> file of <code>express.js</code> is like below.</p>
  
  <pre><code>const express = require('express');
  const bodyParser = require('body-parser');
  const addressroute = require('./api/routes/address');
  const userroute = require('./api/routes/user');
  const mongoose = require('mongoose');
  mongoose.set('useCreateIndex', true);
  mongoose.connect('mongodb://localhost/addresses', { useNewUrlParser: true });
  const db = mongoose.connection;
  
  var cors = require('cors');
  
  
  let app = express();
  app.use(bodyParser.urlencoded( { extended:true } ));
  app.use(bodyParser.json());
  app.use('/api/address', addressroute);
  app.use('/api/users', userroute);
  app.use(cors());
  app.options('*', cors());
  const PORT = process.env.PORT || 4000;
  app.listen(PORT);
  </code></pre>
  
  <p>I installed CORS using <code>npm install cors --save</code> this command.</p>
  
  <p>I am getting below error.</p>
  
  <pre><code>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:4000/api/users/register. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).[Learn More]
  Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:4000/api/users/register. (Reason: CORS request did not succeed).
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/SHzRo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SHzRo.png" alt="enter image description here"></a></p>