Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting express server to accept CORS request

Tags:

I have my express server running on http://localhost:3000 (I call this web-server) I have another application running on localhost:8100 (I call this simply 'app')

When my app makes a call to the webserver I receive the message:

"XMLHTTPReqeust cannot load http://localhost:3000/auth/facebook. Response to preflight request doesn't pass access control check. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' when the credentials flag is true. Origin 'http://localhost:81000' is therefore not allowed acecss"

This message shows up in the browser console.

I have setup the following options in the middleware of my node webserver

res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT, POST,DELETE'); 

After reading few stackoverfow questions, I also added the following:

 res.header('Access-Control-Allow-Origin', 'http://localhost:8100'); 

however this does not resolve the issue.

like image 808
runtimeZero Avatar asked Nov 02 '15 17:11

runtimeZero


People also ask

How do I enable CORS in node js server?

Enable All CORS Requests If you want to enable CORS for all the request you can simply use the cors middleware before configuring your routes: const express = require('express'); const cors = require('cors'); const app = express(); app.

How do I bypass the CORS on Express?

1 Answer. Show activity on this post. Install cors library npm install cors --save and use it like so. I already bye-pass cors with request library.

Why we use CORS in Express?

CORS stands for Cross-Origin Resource Sharing . It allows us to relax the security applied to an API. This is done by bypassing the Access-Control-Allow-Origin headers, which specify which origins can access the API.


1 Answers

I use cors and implement it so, it's very simple

var cors=require('cors');

app.use(cors({origin:true,credentials: true}));

like image 106
juan david restrepo villada Avatar answered Sep 19 '22 06:09

juan david restrepo villada