1

I done so many changes in server.js file to solve this issue CORS request did not succeed but i always facing the same issue.How to solve this issue. I added my app.js code please check anyone. thanks

app.js Code

var express = require('express');

var bodyParser = require("body-parser");
var app = express();
var cors = require('cors');

const userlogin   = require('./routes/User_route');
const deviceRoutes = require("./routes/Device_route");

app.use('*', cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.set('port', process.env.PORT || 4007);
app.set('host', "*.*.*.*"); 

app.use("/users",userlogin);
app.use("/users", deviceRoutes);
module.exports = app;

Error

TypeError: "NetworkError when attempting to fetch resource." bundle.js:132447:16

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:4007/users/login. (Reason: CORS request did not succeed).

Prashant Gupta
  • 788
  • 8
  • 26
  • Possible duplicate of [How to allow CORS?](https://stackoverflow.com/questions/7067966/how-to-allow-cors) – Mike Doe Aug 29 '18 at 06:51
  • Not succeed i try all option in this post @emix – Prashant Gupta Aug 29 '18 at 06:53
  • 1
    You don't need to call `users.use(cors())` in your `User.js` if you've already done it in your `Server.js`. Also, since you are using `cors` plugin, that `app.use` part with `res.setHeader` etc. is redundant. Try to change `app.use(cors())` to `app.use(cors({ origin: ['http://localhost:4007'], credentials: true }))` and see if it works. – dziraf Aug 29 '18 at 07:13
  • Your server code looks okay! I think you need to use JSONP (https://www.npmjs.com/package/jsonp)with requests from react. – Harshal Yeole Aug 29 '18 at 07:33

0 Answers0