Prompt
When working with HTTP responses, follow these principles for proper header management:
- Only set default headers when undefined, not when falsy:
if (!response.getHeader('Content-Type')) { response.setHeader('Content-Type', 'application/octet-stream'); }This preserves intentionally set falsy values while ensuring compatibility between different HTTP adapters like Express and Fastify.
- For specialized protocols like Server-Sent Events (SSE), include all necessary headers for proxy compatibility:
response.writeHead(200, { 'Content-Type': 'text/event-stream; charset=utf-8', 'Cache-Control': 'private, no-cache, no-store, must-revalidate, max-age=0, no-transform', 'X-Accel-Buffering': 'no' });Avoid setting headers that should be determined by the server, such as
Transfer-Encoding. - For redirects, use standard HTTP status codes with 302 (FOUND) as the default:
public redirect(response: any, statusCode: number, url: string) { const code = statusCode ? statusCode : HttpStatus.FOUND; // implementation } - Maintain consistency between different HTTP adapters to ensure predictable behavior across platforms.