3
3
* See README for examples.
4
4
*/
5
5
6
- export default function ( req , res , next ) {
6
+ function ourQuip ( req , res , next ) {
7
7
if ( arguments . length === 1 ) {
8
8
res = req ;
9
9
req = null ;
@@ -16,33 +16,43 @@ export default function (req, res, next) {
16
16
///// private helper methods /////
17
17
var withStatus = function ( code ) {
18
18
return function ( data ) {
19
- return data ? res . status ( code ) . send ( data ) :
20
- res . status ( code ) ;
19
+ return data ? res . status ( code ) . send ( data ) : res . status ( code ) ;
21
20
} ;
22
21
} ;
23
22
var redirection = function ( code , message ) {
24
23
return function ( loc ) {
25
24
res . _quip_headers . Location = loc ;
26
- return res . status ( code ) . send (
27
- '<html>' +
28
- '<head>' +
29
- '<title>' + code + ' ' + message + '</title>' +
30
- '</head>' +
31
- '<body>' +
32
- '<p>' +
33
- message + ': ' +
34
- '<a href="' + loc + '">' + loc + '</a>' +
35
- '</p>' +
36
- '</body>' +
37
- '</html>'
38
- ) ;
25
+ return res
26
+ . status ( code )
27
+ . send (
28
+ "<html>" +
29
+ "<head>" +
30
+ "<title>" +
31
+ code +
32
+ " " +
33
+ message +
34
+ "</title>" +
35
+ "</head>" +
36
+ "<body>" +
37
+ "<p>" +
38
+ message +
39
+ ": " +
40
+ '<a href="' +
41
+ loc +
42
+ '">' +
43
+ loc +
44
+ "</a>" +
45
+ "</p>" +
46
+ "</body>" +
47
+ "</html>"
48
+ ) ;
39
49
} ;
40
- }
50
+ } ;
41
51
var withType = function ( type ) {
42
52
return function ( data ) {
43
- res . headers ( { ' Content-Type' : type } ) ;
44
- return data ? res . send ( data ) : res ;
45
- }
53
+ res . headers ( { " Content-Type" : type } ) ;
54
+ return data ? res . send ( data ) : res ;
55
+ } ;
46
56
} ;
47
57
48
58
///// exported methods /////
@@ -64,8 +74,8 @@ export default function (req, res, next) {
64
74
res . noContent = withStatus ( 204 ) ;
65
75
66
76
// redirection
67
- res . moved = redirection ( 301 , ' Moved Permanently' ) ;
68
- res . redirect = redirection ( 302 , ' Found' ) ;
77
+ res . moved = redirection ( 301 , " Moved Permanently" ) ;
78
+ res . redirect = redirection ( 302 , " Found" ) ;
69
79
res . found = res . redirect ;
70
80
res . notModified = function ( ) {
71
81
res . status ( 304 ) . send ( ) ;
@@ -81,32 +91,32 @@ export default function (req, res, next) {
81
91
res . gone = withStatus ( 410 ) ;
82
92
83
93
// server error
84
- res . error = withStatus ( 500 , ' error' ) ;
94
+ res . error = withStatus ( 500 , " error" ) ;
85
95
86
96
// mime types
87
- res . text = withType ( ' text/plain' ) ;
97
+ res . text = withType ( " text/plain" ) ;
88
98
res . plain = res . text ;
89
- res . html = withType ( ' text/html' ) ;
90
- res . xhtml = withType ( ' application/xhtml+xml' ) ;
91
- res . css = withType ( ' text/css' ) ;
92
- res . xml = withType ( ' text/xml' ) ;
93
- res . atom = withType ( ' application/atom+xml' ) ;
94
- res . rss = withType ( ' application/rss+xml' ) ;
95
- res . javascript = withType ( ' application/javascript' ) ;
96
- res . json = withType ( ' application/json' ) ;
99
+ res . html = withType ( " text/html" ) ;
100
+ res . xhtml = withType ( " application/xhtml+xml" ) ;
101
+ res . css = withType ( " text/css" ) ;
102
+ res . xml = withType ( " text/xml" ) ;
103
+ res . atom = withType ( " application/atom+xml" ) ;
104
+ res . rss = withType ( " application/rss+xml" ) ;
105
+ res . javascript = withType ( " application/javascript" ) ;
106
+ res . json = withType ( " application/json" ) ;
97
107
98
108
// custom mime type
99
109
res . mime = function ( type , data ) {
100
- res . headers ( { ' Content-Type' : type } ) ;
101
- return data ? res . send ( data ) : res ;
110
+ res . headers ( { " Content-Type" : type } ) ;
111
+ return data ? res . send ( data ) : res ;
102
112
} ;
103
113
104
114
// JSONP is a special case that should always respond with a 200,
105
115
// there is no reliable way to receive a JSONP result on the
106
116
// client-side if the HTTP status-code is not 200!
107
117
res . jsonp = function ( callback , data ) {
108
- if ( typeof data == ' object' ) data = JSON . stringify ( data ) ;
109
- data = callback + '(' + data + ');' ;
118
+ if ( typeof data == " object" ) data = JSON . stringify ( data ) ;
119
+ data = callback + "(" + data + ");" ;
110
120
return res . ok ( ) . javascript ( data ) ;
111
121
} ;
112
122
@@ -122,28 +132,26 @@ export default function (req, res, next) {
122
132
res . send = function ( data ) {
123
133
if ( data ) {
124
134
if ( Buffer . isBuffer ( data ) ) {
125
- res . _quip_headers [ 'Content-Length' ] = data . length
126
- }
127
- else {
128
- if ( typeof data === 'object' ) {
135
+ res . _quip_headers [ "Content-Length" ] = data . length ;
136
+ } else {
137
+ if ( typeof data === "object" ) {
129
138
// assume data is JSON if passed an object (not a buffer)
130
- if ( ! res . _quip_headers [ ' Content-Type' ] ) {
131
- res . _quip_headers [ ' Content-Type' ] = ' application/json' ;
139
+ if ( ! res . _quip_headers [ " Content-Type" ] ) {
140
+ res . _quip_headers [ " Content-Type" ] = " application/json" ;
132
141
}
133
142
data = JSON . stringify ( data ) ;
134
143
}
135
- res . _quip_headers [ ' Content-Length' ] = Buffer . byteLength ( data ) ;
144
+ res . _quip_headers [ " Content-Length" ] = Buffer . byteLength ( data ) ;
136
145
}
137
146
}
138
- if ( ! res . _quip_headers [ ' Content-Type' ] ) {
147
+ if ( ! res . _quip_headers [ " Content-Type" ] ) {
139
148
// assume HTML if data is a string and content type not set
140
- res . _quip_headers [ ' Content-Type' ] = ' text/html' ;
149
+ res . _quip_headers [ " Content-Type" ] = " text/html" ;
141
150
}
142
151
143
152
if ( data ) {
144
153
res . write ( data ) ;
145
- }
146
- else {
154
+ } else {
147
155
res . writeHead ( res . _quip_status , res . _quip_headers ) ;
148
156
}
149
157
res . end ( ) ;
@@ -153,10 +161,10 @@ export default function (req, res, next) {
153
161
if ( next ) {
154
162
// pass updated response object onto next connect middleware
155
163
next ( null , res ) ;
156
- }
157
- else {
164
+ } else {
158
165
// called directly, return updated response object
159
166
return res ;
160
167
}
168
+ }
161
169
162
- } ;
170
+ export default ourQuip ;
0 commit comments