UNPKG

1.12 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('../base/Base');
7
8module.exports = class Response extends Base {
9
10 code = 200;
11
12 has () {
13 return !!this.method;
14 }
15
16 redirect (url) {
17 this.method = 'redirect';
18 this.data = url || '';
19 }
20
21 send (method, data, code) {
22 this.method = method;
23 this.data = data;
24 this.code = code || this.code;
25 }
26
27 sendData (data, encoding) {
28 this.method = 'end';
29 this.data = data;
30 this.encoding = encoding;
31 }
32
33 end () {
34 const res = this.controller.res;
35 if (res.headersSent) {
36 const {method, originalUrl} = this.controller.req;
37 return this.controller.log('error', `Headers already sent: ${method}: ${originalUrl}`);
38 }
39 if (this.code) {
40 res.status(this.code);
41 }
42 !this.method || this.method === 'end'
43 ? res.end(this.data, this.encoding)
44 : res[this.method](this.data);
45 }
46};
\No newline at end of file