@@ -19,34 +19,37 @@ export function writeResultToResponse(
19
19
// result returned back from invoking controller method
20
20
result : OperationRetval ,
21
21
) : void {
22
- if ( result ) {
23
- if ( result instanceof Readable || typeof result . pipe === 'function' ) {
24
- response . setHeader ( 'Content-Type' , 'application/octet-stream' ) ;
25
- // Stream
26
- result . pipe ( response ) ;
27
- return ;
28
- }
29
- switch ( typeof result ) {
30
- case 'object' :
31
- case 'boolean' :
32
- case 'number' :
33
- if ( Buffer . isBuffer ( result ) ) {
34
- // Buffer for binary data
35
- response . setHeader ( 'Content-Type' , 'application/octet-stream' ) ;
36
- } else {
37
- // TODO(ritch) remove this, should be configurable
38
- // See https://github.com/strongloop/loopback-next/issues/436
39
- response . setHeader ( 'Content-Type' , 'application/json' ) ;
40
- // TODO(bajtos) handle errors - JSON.stringify can throw
41
- result = JSON . stringify ( result ) ;
42
- }
43
- break ;
44
- default :
45
- response . setHeader ( 'Content-Type' , 'text/plain' ) ;
46
- result = result . toString ( ) ;
47
- break ;
48
- }
49
- response . write ( result ) ;
22
+ if ( ! result ) {
23
+ response . statusCode = 204 ;
24
+ response . end ( ) ;
25
+ return ;
50
26
}
51
- response . end ( ) ;
27
+
28
+ if ( result instanceof Readable || typeof result . pipe === 'function' ) {
29
+ response . setHeader ( 'Content-Type' , 'application/octet-stream' ) ;
30
+ // Stream
31
+ result . pipe ( response ) ;
32
+ return ;
33
+ }
34
+ switch ( typeof result ) {
35
+ case 'object' :
36
+ case 'boolean' :
37
+ case 'number' :
38
+ if ( Buffer . isBuffer ( result ) ) {
39
+ // Buffer for binary data
40
+ response . setHeader ( 'Content-Type' , 'application/octet-stream' ) ;
41
+ } else {
42
+ // TODO(ritch) remove this, should be configurable
43
+ // See https://github.com/strongloop/loopback-next/issues/436
44
+ response . setHeader ( 'Content-Type' , 'application/json' ) ;
45
+ // TODO(bajtos) handle errors - JSON.stringify can throw
46
+ result = JSON . stringify ( result ) ;
47
+ }
48
+ break ;
49
+ default :
50
+ response . setHeader ( 'Content-Type' , 'text/plain' ) ;
51
+ result = result . toString ( ) ;
52
+ break ;
53
+ }
54
+ response . end ( result ) ;
52
55
}
0 commit comments