http-response.js (565B)
1 export class HttpResponse { 2 constructor(status, body, headers = {}) { 3 this.status = status; 4 this.body = body; 5 this.headers = headers; 6 } 7 8 send(res) { 9 if (this.body === undefined) { 10 res.writeHead(this.status, this.headers); 11 res.end(); 12 return; 13 } 14 15 const isText = typeof this.body === 'string'; 16 res.writeHead(this.status, { 17 'content-type': isText ? 'text/plain; charset=utf-8' : 'application/json; charset=utf-8', 18 ...this.headers, 19 }); 20 res.end(isText ? this.body : JSON.stringify(this.body)); 21 } 22 }