When working with HTTP responses, follow these principles for proper header management: 1. **Only set default headers when undefined, not when falsy:**
When working with HTTP responses, follow these principles for proper header management:
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.
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
.
public redirect(response: any, statusCode: number, url: string) {
const code = statusCode ? statusCode : HttpStatus.FOUND;
// implementation
}
Enter the URL of a public GitHub repository