Cors issue when rest api application server(expres

2019-06-24 10:47发布

I have rest api application written in node.js & express is running on port 3000 and angularjs application running on port 9001 on same server . On calling rst api from angularjs application it is giving cors issue .

In rest api application , I have used "cors" module

var cors = require('cors');

app.use(cors());

But it is giving me following error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/api/user/login. This can be fixed by moving the resource to the same domain or enabling CORS

Request header and response header is as follow

Response header

Content-Length 222
Content-Type application/json; charset=utf-8
Date Tue, 16 Dec 2014 08:40:05 GMT
X-Powered-By Express

view source

Request Header

Accept application/json, text/plain, /
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Authorization null
Content-Length 47
Content-Type application/json;charset=utf-8
Host localhost:3000
Origin http://localhost:9001
Referer http://localhost:9001/
User-Agent Mozilla/5.0 (Windows NT 6.2; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0

Thanks

2条回答
Root(大扎)
2楼-- · 2019-06-24 11:44

I solved it by adding adding line app.options('*', cors()); just before all routes.

It was an issue of preflight request with http-request-header "OPTIONS"

查看更多
何必那么认真
3楼-- · 2019-06-24 11:53

You can set the solution to be

app.configure('development', function(){
    app.set('origins', '*:*');
}
查看更多
登录 后发表回答