title: Request from Rest API using axios
body: <p>I created a Rest API using express that connects to mongodb.</p>
  
  <p>It works perfectly with postman.</p>
  
  <p>Then i created a simple web app with vue, and tried to get a response from the api using axios, but i get an error:</p>
  
  <blockquote>
    <p>Access to XMLHttpRequest at ... has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.</p>
  </blockquote>
  
  <p>i tried using multiple solutions including <a href="https://github.com/expressjs/cors" rel="nofollow noreferrer"><strong>cors</strong></a>, and setting res.header.</p>
  
  <p><strong><em>index.js</em></strong></p>
  
  <pre><code>const app = express()
  
  app.use(function (req, res, next) {
      res.header('Access-Control-Allow-Origin', '*')
      res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
      next()
  })
  
  app.use(bodyParser.json())
  
  app.use('/api', require('./routes/api'))
  
  app.use((err, req, res, next) =&gt; {
      console.log(err)
      res.status(422).send({error: err.message + req.params})
  })
  
  app.listen(process.env.port || 4000, () =&gt; {
      console.log('listening...')
  })
  </code></pre>
  
  <p><strong><em>vue component</em></strong></p>
  
  <pre><code>import axios from 'axios'
      export default {
          data: () =&gt; ({
              info: 'omg',
          }),
          mounted() {
              axios.get('localhost:4000')
                   .then(response =&gt; (this.info = response))
                   .catch(error =&gt; console.log(error))
          },
      }
  </code></pre>
  
  <p><strong>How can i get data from the api?</strong></p>