-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiExpress_response.js
116 lines (101 loc) · 3.21 KB
/
miniExpress_response.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var fileExtensionsToContentType = require('./miniHttp_constants').fileExtensionsToContentType;
function Response(serverResponse) {
var that = this;
// Take attributes from the given serverResponse instance
for (prop in serverResponse) {
this[prop] = serverResponse[prop];
}
this.set =
this.header = function(field, value){
if (value !== undefined) {
that.setHeader(field, value);
} else {
for (var key in field) {
that.setHeader(key, field[key]);
}
}
return that;
};
this.status = function (code) {
that.statusCode = code;
return that;
};
this.get = function (field) {
return that.getHeader(field);
};
this.cookie = function (name, value, options) {
options = (options) ? options : {};
if (typeof value === 'object') {
value = 'j:' + JSON.stringify(value);
}
if ('maxAge' in options) {
options.expires = new Date(Date.now() + options.maxAge);
options.maxAge /= 1000;
}
if (options.path === undefined) {
options.path = '/';
}
var cookieHeaderValue = [name + '=' + value];
if (options.maxAge) {
cookieHeaderValue.push('max-age=' + options.maxAge);
}
if (options.domain) {
cookieHeaderValue.push('domain=' + options.domain);
}
if (options.path) {
cookieHeaderValue.push('path=' + options.path);
}
if (options.expires) {
cookieHeaderValue.push('expires=' + options.expires.toUTCString());
}
if (options.httpOnly) {
cookieHeaderValue.push('httponly');
}
if (options.secure) {
cookieHeaderValue.push('secure');
}
cookieHeaderValue = cookieHeaderValue.join('; ');
that.set('set-cookie', cookieHeaderValue);
return that;
};
this.send = function () {
var body = arguments[0];
if (arguments.length >= 2) {
that.statusCode = arguments[0];
body = arguments[1];
}
switch (typeof body) {
case 'number':
that.statusCode = body;
that.writeResponse();
break;
case 'string':
if (!that.get('Content-Type')) {
that.set('Content-Type', fileExtensionsToContentType.html); // defaulting to html
}
that.set('Content-Length', body.length);
that.body = body;
that.writeResponse();
break;
case 'object':
if (Buffer.isBuffer(body)) {
that.send(body.toString())
} else {
that.json(body);
}
break;
}
return that;
};
this.json = function () {
var body = arguments[0];
if (arguments.length == 2) {
that.statusCode = body;
body = arguments[1];
}
body = JSON.stringify(body);
that.set('Content-Type', 'application/json');
return that.send(body);
};
}
module.exports = Response;