loading...

CORS error hack

dkp1903 profile image Dushyant Pathak ・1 min read

Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell browsers to give a web application running at one origin, access to selected resources from a different origin.

A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, or port) from its own.

An example of a cross-origin request: the front-end JavaScript code served from https://a.com uses XMLHttpRequest to make a request for https://b.com/data.json.

More details on CORS can be found here.

Most noobs, who are starting with Back-End development, will often see this error.

CORS

The first step would be to use the CORS package, when working with NPM.

//Install
npm i cors

//In index.js file,
var cors = require('cors');
app.use('cors');
Enter fullscreen mode Exit fullscreen mode

Some deployments may still cause the CORS error in some cases, wherein this particular Chrome extension comes handy.

CORS Extension

You just have to Enable and save, and it should solve the CORS error for you.

However, do note that some sites, usually third party API services, such as Newsapi restrict only localhost sites to be able to access their free plan APIs, and will block access requests from deployed websites.

Cheers! Have a CORS-Free day!

Discussion

pic
Editor guide

Some comments have been hidden by the post's author - find out more