nosdav / server

NosDAV Server
https://nosdav.com/server
MIT License
9 stars 1 forks source link

Refactor: Move CORS header setting into a separate function #1

Closed melvincarvalho closed 1 year ago

melvincarvalho commented 1 year ago

Refactoring suggestion

Currently, the CORS headers are set directly within the request handler like this:

res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, PUT, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');

I suggest encapsulating this logic into a separate function, for example, called setCorsHeaders:

function setCorsHeaders(res) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, PUT, OPTIONS');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
}

And using the function in the request handler:

setCorsHeaders(res);

Benefits

Additional Information

No breaking changes are introduced as this refactoring is purely an internal code improvement.