0

I had this fixed by adding app.use(cors()) on my index page of my server, but now that I have added a mongodb URI from mlab I seem to be having an issue with it.

I am getting this error: XMLHttpRequest cannot load http://localhost:3000/posts. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

Here is my index.js:

require('babel-core/register')({
  "presets":["es2015", "react", "stage-1"]
});

const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const router = require('./router');
const mongoose = require('mongoose');
const cors = require('cors');
const path = require('path');
var favicon = require('serve-favicon');


//DB setup
mongoose.connect('mongodb://****:****@****:****/*****')
mongoose.Promise = global.Promise;
//App setup
const app = express();
app.use(express.static(__dirname + "../client/public"));
// app.use(morgan('combined'));
router(app);
app.use(cors());
app.use(bodyParser.json({type: '*/*'}))
//Server setup
const port = process.env.PORT || 3000;
app.listen(port, function() {
  console.log('Server listening on: ', port);
});
jrenk
  • 1,387
  • 3
  • 24
  • 46
  • 3
    I'm not sure, but i think you should add the cors and bodyParser middleware before your route declarations. – Thomas Jul 12 '17 at 14:33
  • I tried that and still getting same error =/ –  Jul 12 '17 at 14:40
  • You run your react-app on port 8080, right? Then perhaps this post helps: https://stackoverflow.com/a/10892392/4629808 – Thomas Jul 12 '17 at 14:50
  • It was working just fine with the localhost mongolab. It's when I added the mongodb URI from mlab. The `app.use(cors())` was taking care of it –  Jul 12 '17 at 14:57
  • I tried all their stuff and could not get it to work, still same problem –  Jul 12 '17 at 16:28

0 Answers0