1 |
|
2 | var events = require('events');
|
3 |
|
4 |
|
5 |
|
6 | var Request = exports.Request = function(body) {
|
7 | if (typeof body === 'string') {
|
8 | body = JSON.parse(body);
|
9 | }
|
10 | this.body = body || { };
|
11 | };
|
12 |
|
13 |
|
14 |
|
15 | var Response = exports.Response = function() {
|
16 | this.status = 200;
|
17 | this.response = '';
|
18 | this.redirectedTo = null;
|
19 | };
|
20 |
|
21 | Response.prototype = new events.EventEmitter();
|
22 |
|
23 | Response.prototype.json = function(response, status) {
|
24 | if (typeof response === 'number' && ! status) {
|
25 | this.status = response;
|
26 | } else {
|
27 | this.response = JSON.stringify(response);
|
28 | this.status = status || 200;
|
29 | }
|
30 | this.emit('respond', this);
|
31 | };
|
32 |
|
33 | Response.prototype.send = function(response, status) {
|
34 | if (typeof response === 'number' && ! status) {
|
35 | this.status = response;
|
36 | } else {
|
37 | this.response = String(response);
|
38 | this.status = status || 200;
|
39 | }
|
40 | this.emit('respond', this);
|
41 | };
|
42 |
|
43 | Response.prototype.redirect = function(redirectTo, status) {
|
44 | this.redirectedTo = redirectTo;
|
45 | this.status = status || 302;
|
46 | this.emit('respond', this);
|
47 | };
|
48 |
|