DEV Community

Dushyant Pathak
Dushyant Pathak

Posted on

4 3

CORS error hack

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!

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs