Refactor (src/middleware/headers.js:12): Function with high complexity (count = 34): exports #251
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
P1B: Starter Task: Refactoring PR
1. Issue
#242
/workspaces/NodeBB/src/middleware/headers.js
What do you think this file does?
It sets security/cors headers, adds a devonly header, and auto detects guest language.
What is the scope of your refactoring within that file?
Header and locale logic were split into small helpers, inline handlers removed, and exports simplified.
Which Qlty‑reported issue did you address?
The main function’s complexity (34) was reduced and now exports are simple with no warnings.
2. Refactoring
How did the specific issue you chose impact the codebase’s adaptability?
One large function made headers logic hard to read, change, or test.
What changes did you make to resolve the issue?
Split logic into small helpers buildBaseHeaders, applyCspHeaders, CORS helpers, applyAdditionalHeaders, writeHeaders. Created addHeadersHandler and autoLocaleHandler. Split locale flow into handleLangQuery and maybeDetectAnonLang. Kept listCodes as a shared helper.
How do your changes improve adaptability? Did you consider alternatives?
Easier to change one piece (e.g., CORS) without touching others.
3. Validation
How did you trigger the refactored code path from the UI?
Accessed NodeBB Through Browser and code path triggered automatically.
Attach a screenshot of the logs and UI demonstrating the trigger.

(Run
./nodebb log; include the relevant UI view. Temporary logs should be removed before final commit.)WARNING: The keyword 'none' must be used as a single argument.
../../../bootstrap/scss/mixins/_box-shadow.scss 10:9 box-shadow()
../../../bootstrap/scss/forms/_form-select.scss 32:7 @import
bootstrap/scss/_forms.scss 4:9 @import
- 19:9 root stylesheet
Attach a screenshot of

qlty smells --no-snippets <full/path/to/file.js>showing fewer reported issues after the changes.