File tree Expand file tree Collapse file tree 4 files changed +21
-71
lines changed Expand file tree Collapse file tree 4 files changed +21
-71
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,6 @@ This isn't another wrapper around [Express](http://expressjs.com/) or a framewor
20
20
* Automatic pagination, sorting, filtering via query params in controllers
21
21
* CLI for eliminating boiler plate
22
22
* [ JSON API] ( http://jsonapi.org/ ) 1.0 compliant out of the box
23
- * Chunked/Streamed JSON responses to client over http
24
23
* Optimized database queries based on serialized attributes and associations
25
24
* Highly extensible - just write reusable JavaScript functions
26
25
* Pairs nicely with client-side JavaScript applications 🍷
Original file line number Diff line number Diff line change 2
2
import http from 'http' ;
3
3
import { parse as parseURL } from 'url' ;
4
4
5
- import responder from './responder' ;
5
+ import { createResponder } from './responder' ;
6
6
7
7
import entries from '../../utils/entries' ;
8
8
import tryCatch from '../../utils/try-catch' ;
@@ -70,6 +70,7 @@ class Server {
70
70
71
71
receiveRequest ( req : IncomingMessage , res : ServerResponse ) : void {
72
72
const startTime = Date . now ( ) ;
73
+ const respond = createResponder ( req , res ) ;
73
74
74
75
tryCatch ( async ( ) => {
75
76
const { logger } = this ;
@@ -108,22 +109,8 @@ class Server {
108
109
startTime
109
110
} ) ;
110
111
111
- this . sendResponse ( req , res , await this . router . visit ( req , res ) ) ;
112
- } , err => {
113
- this . sendResponse ( req , res , err ) ;
114
- } ) ;
115
- }
116
-
117
- sendResponse (
118
- req : IncomingMessage ,
119
- res : ServerResponse ,
120
- data : void | ?mixed
121
- ) : void {
122
- if ( data && typeof data . pipe === 'function' ) {
123
- data . pipe ( res ) ;
124
- } else {
125
- responder. resolve ( req , res, data) ;
126
- }
112
+ respond ( await this . router . visit ( req , res ) ) ;
113
+ } , respond ) ;
127
114
}
128
115
}
129
116
Original file line number Diff line number Diff line change 1
1
// @flow
2
- import Response from './response' ;
3
-
4
2
import normalize from './utils/normalize' ;
5
3
6
4
import type { IncomingMessage , ServerResponse } from 'http' ;
7
5
8
- export function resolve (
6
+ export function createResponder (
9
7
req : IncomingMessage ,
10
- res : ServerResponse ,
11
- data : ?mixed | void
12
- ) : void {
13
- new Response ( )
14
- . once ( 'ready' , ( stream : Response ) = > {
15
- const { normalized, ...meta } = normalize ( data ) ;
16
- let { statusCode } = meta ;
8
+ res : ServerResponse
9
+ ) : ( data : ?mixed | void ) => void {
10
+ return function respond ( data : ?mixed | void ) : void {
11
+ const { normalized, ...meta } = normalize ( data ) ;
12
+ let { statusCode } = meta ;
17
13
18
- if ( statusCode === 200 && req . method === 'POST' ) {
19
- statusCode ++ ;
20
- }
14
+ if ( statusCode === 200 && req . method === 'POST' ) {
15
+ statusCode ++ ;
16
+ }
21
17
22
- res . statusCode = statusCode ;
23
- stream . end ( normalized ) ;
24
- } )
25
- . pipe ( res ) ;
26
- }
18
+ res . statusCode = statusCode ;
27
19
28
- export default {
29
- resolve
30
- } ;
20
+ if ( typeof normalized === 'string' ) {
21
+ res . end ( normalized ) ;
22
+ } else {
23
+ res. end ( JSON . stringify ( normalized ) ) ;
24
+ }
25
+ } ;
26
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments