mirror of
https://github.com/sergeyyarkov/node-docker-example.git
synced 2026-10-11 02:09:22 +03:00
30 lines
607 B
JavaScript
30 lines
607 B
JavaScript
import http from 'node:http';
|
|
|
|
class Response {
|
|
/**
|
|
* Extends the http response object with this class
|
|
* @param {http.ClientRequest} req
|
|
* @param {http.ServerResponse} res
|
|
*/
|
|
constructor(req, res) {
|
|
this.req = req;
|
|
this.res = res;
|
|
}
|
|
|
|
send(text, status = 200) {
|
|
this.res.writeHead(status, { 'Content-Type': 'text/plain' });
|
|
this.res.end(text);
|
|
}
|
|
|
|
json(data, status = 200) {
|
|
this.res.writeHead(status, { 'Content-Type': 'application/json' });
|
|
this.res.end(JSON.stringify(data));
|
|
}
|
|
|
|
notFound(text) {
|
|
this.send(text, 404);
|
|
}
|
|
}
|
|
|
|
export default Response;
|