title: Angular 6 and Nodejs: No 'Access-Control-Allow-Origin' header is present on the requested resource
body: <p>I have form in  angular 6 app and nodejs, when I submit the form I get the following error:</p>
  
  <p><code>Failed to load http://localhost:3000/contact/send: 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.
  </code>
  I searched and checked other similar errors but I could not manage to get rid of the error :(</p>
  
  <p>Here is server.js</p>
  
  <pre><code>    // server.js
      const express = require('express');
      const bodyParser = require('body-parser');
      const cors = require('cors');
      const path = require('path');
      const app = express();
      // Port Number
      const port = process.env.PORT || 3000
      // Run the app by serving the static files
      // in the dist directory
      app.use(express.static(path.join(__dirname, '/majeni/dist/majeni')));
      // Body Parser Middleware
      app.use(bodyParser.urlencoded({ extended: false }));
      app.use(bodyParser.json());
      //routes
      const contact = require('./app/routes/contact');
      app.use('/contact', contact);
      // CORS Middleware
      app.use(cors());
      // If an incoming request uses
      // a protocol other than HTTPS,
      // redirect that request to the
      // same url but with HTTPS
      const forceSSL = function () {
        return function (req, res, next) {
          if (req.headers['x-forwarded-proto'] !== 'https') {
            return res.redirect(
              ['https://', req.get('Host'), req.url].join('')
            );
          }
          next();
        }
      }
  
      // Instruct the app
      // to use the forceSSL
      // middleware
      app.use(forceSSL());
  
      // For all GET requests, send back index.html
      // so that PathLocationStrategy can be used
      app.get('/*', function (req, res) {
        res.sendFile(path.join(__dirname + '/majeni/dist/majeni/index.html'));
      });
  
      // Start Server
      app.listen(port, () =&gt; {
          console.log('Server started on port '+port);
        });
  </code></pre>
  
  <p>and here is the routes (contact.js for node mailer)</p>
  
  <pre><code>const express = require('express');
  const router = express.Router();
  const request = require('request');
  const nodemailer = require('nodemailer');
  
  router.get('/send', (req, res) =&gt; {
      const outputData = `
      &lt;p&gt;You have a new contact request&lt;/p&gt;
      &lt;h3&gt;Contact Details&lt;/h3&gt;
      &lt;ul&gt;  
        &lt;li&gt;Name: ${req.body.name}&lt;/li&gt;
        &lt;li&gt;Email: ${req.body.email}&lt;/li&gt;
      &lt;/ul&gt;
      &lt;h3&gt;Message&lt;/h3&gt;
      &lt;p&gt;${req.body.message}&lt;/p&gt;
    `;
  
      let transporter = nodemailer.createTransport({
          service: 'gmail',
          secure: false,
          port: 25,
          auth: {
              user: 'MY EMAIL',
              pass: 'THE PASSWORD'
          },
          tls: {
              rejectUnauthorized: false
          }
      });
  
      let HelperOptions = {
          from: '"MYNAME" &lt;MYEMAIL,
          to: 'MYEMAIL',
          subject: 'Majeni Contact Request',
          text: 'Hello',
          html: outputData
      };
  
  
  
      transporter.sendMail(HelperOptions, (error, info) =&gt; {
          if (error) {
              return console.log(error);
          }
          console.log("The message was sent!");
          console.log(info);
      });
  
  });
  module.exports = router;
  </code></pre>
  
  <p>Note: i have installed cors via npm and tried other method but nothing .</p>
  
  <p>what am I missing in my code?</p>