How to remove Cors error in node js

Cors Error is a very basic problem in APIs it’s a kind of security measure that browser is blocking API requests.

Error will look like above image,

STEP 1:

go to your node js code and install cors

npm install cors

STEP 2:

import cors by

const cors = require(‘cors’);

STEP 3:

write this code for allowing requests

app.use(cors());

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Methods",'GET,PUT,DELETE,POST');
    next();
});

STEP 4:

npm start

you will see problem fixed.

METHOD 2:

this method is browser side method in this you will be installing one extension that will take care of that cors error

https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en-US

install above extension

STEP 1 :

after installing the extension turn it on when youre on the website or webapp you want to remove error

then you will see problem got solved

that’s it guys let me know if youre still facing any problems in comment section.

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock