CORS – Cross-Origin Resource-Sharing is my best friend

Deploying the backend separately from the frontend (on different servers) has to deal with CORS, the Cross-Origin fun. Personally, CORS and I are not best friends but we are getting along with each other.

What does CORS do? CORS enables secure requests and data transfers from server to client and vise-versa. It is best to configure CORS on the backend server side instead of doing all the magic in the frontend (potential leading to severe security issues).

For a node-js application (as I’m developing one at the moment), CORS can be easily configured for express with the cors package.

I’ve defined CORS with the following attributes, depending on the backend and the use case

const corsOption = {
origin: ['http://localhost:3000'],
methods: ['POST', 'GET', 'DELETE', 'PUT'],
allowedHeaders: ['Content-Type', 'Authorization'],
};

Share this content:

CATEGORIES:

Learnings

No responses yet

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Latest Comments