/
response.js
84 lines (75 loc) · 2.28 KB
/
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
var ServerResponse = require('http').ServerResponse,
STATUS_CODES = require('http').STATUS_CODES,
fs = require('fs'),
path = require('path'),
mime = require('mime'),
inherit = require('../utils').inherit;
var EMPTY = new Buffer(0);
module.exports = Response;
function Response(callback){
this.callback = callback;
}
inherit(Response, ServerResponse, [
function end(data){
return this.send(data);
},
function send(status, mimetype, data){
if (arguments.length === 1) {
if (typeof status === 'number') {
this.status = status;
} else {
this.mimetype = 'text/html';
this.data = status;
}
} else if (arguments.length === 2){
this.status = status;
this.data = Buffer.isBuffer(mimetype) ? mimetype : new Buffer(mimetype);
} else {
this.status = status;
this.mimetype = mimetype;
this.data = Buffer.isBuffer(data) ? data : new Buffer(data);
}
this.dispatch();
},
function dispatch(){
this.status || (this.status = 200);
this.mimetype || (this.mimetype = 'text/html');
this.data || (this.data = EMPTY);
this.responseCode = STATUS_CODES[this.status];
if (!Buffer.isBuffer(this.data)) {
if (this.data !== null && typeof this.data === 'object') {
this.mimetype = 'application/json';
this.data = new Buffer(JSON.stringify(this.data));
} else if (typeof this.data === 'string' || typeof this.data === 'number') {
this.data = new Buffer(this.data);
} else {
this.data = EMPTY;
}
}
this.callback(this.status, this.responseCode, this.mimetype, this.data);
},
function sendFile(status, filepath){
var self = this;
if (arguments.length === 2) {
this.path = path.resolve(filepath);
this.status = status;
} else {
this.path = path.resolve(status);
}
fs.stat(this.path, function(err, stat){
if (err || !stat.isFile()) {
self.send(404);
} else {
self.mimetype = mime.lookup(this.path);
fs.readFile(self.path, function(err, buffer){
if (err) {
self.send(500);
} else {
self.data = buffer;
self.dispatch();
}
});
}
});
}
]);