HTTP
ServerResponse.setHeader()
Set a response header before it is sent; use the correct media type for error and success responses. Local Node 22.16.0; production target compatibility pending. See https://nodejs.org/download/release/v22.16.0/docs/api/http.html.
Formal Syntax & Structure
nodejs syntaxnodejs
response.setHeader(name, value)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Header field name. |
| value | string | number | string[] | Header value. |
Return Value
ServerResponse
Result or receiver of the documented operation; see the linked versioned API contract.
Working Example
Example Snippetnodejs
function validateOrder(value){ if(!value||typeof value!=='object'||Array.isArray(value)||Object.keys(value).length!==1||!Number.isInteger(value.quantity)||value.quantity<1||value.quantity>20) return false; return true;}function invalidOrder(){return {type:'https://example.test/problems/invalid-order',title:'Invalid order',status:422,detail:'quantity must be an integer from 1 to 20; no other fields allowed'};}for(const value of [{quantity:2},{quantity:'2'},{quantity:21},{quantity:1,admin:true}]) console.log(validateOrder(value));