Skip to content Skip to sidebar Skip to footer

Cant Enable Cors On The Server Side Nodejs

I can't enable CORS on the server-side. My frontend and backend servers have different ports. Here is how server-side is implemented: http .createServer(function (req, res) {

Solution 1:

You explicitly disallowed CORS on the client side by setting mode: 'same-origin' instead of the default mode: 'cors'.

To quote the docs:

same-origin — If a request is made to another origin with this mode set, the result is simply an error. You could use this to ensure that a request is always being made to your origin.

Since http://localhost:3035/is another origin than http://localhost:3030/, the result is, exactly as designed, "simply an error".

Set it to mode: 'cors' or remove mode entirely since cors is the default anyway.


On a side note, Access-Control-Request-Method is a request header in the preflight request, not a response header. You should remove it.


As mentioned in the comments: For a credentialed request to work, you cannot use an allowed origin of *. If you don't want to hardcode the expected origin at this point though, you can avoid this problem by always returning the origin that the current request comes from, using res.setHeader('Access-Control-Allow-Origin', req.headers.origin).

Post a Comment for "Cant Enable Cors On The Server Side Nodejs"