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
2020* Automatic pagination, sorting, filtering via query params in controllers
2121* CLI for eliminating boiler plate
2222* [ JSON API] ( http://jsonapi.org/ ) 1.0 compliant out of the box
23- * Chunked/Streamed JSON responses to client over http
2423* Optimized database queries based on serialized attributes and associations
2524* Highly extensible - just write reusable JavaScript functions
2625* Pairs nicely with client-side JavaScript applications 🍷
Original file line number Diff line number Diff line change 22import http from 'http' ;
33import { parse as parseURL } from 'url' ;
44
5- import responder from './responder' ;
5+ import { createResponder } from './responder' ;
66
77import entries from '../../utils/entries' ;
88import tryCatch from '../../utils/try-catch' ;
@@ -70,6 +70,7 @@ class Server {
7070
7171 receiveRequest ( req : IncomingMessage , res : ServerResponse ) : void {
7272 const startTime = Date . now ( ) ;
73+ const respond = createResponder ( req , res ) ;
7374
7475 tryCatch ( async ( ) => {
7576 const { logger } = this ;
@@ -108,22 +109,8 @@ class Server {
108109 startTime
109110 } ) ;
110111
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 ) ;
127114 }
128115}
129116
Original file line number Diff line number Diff line change 11// @flow
2- import Response from './response' ;
3-
42import normalize from './utils/normalize' ;
53
64import type { IncomingMessage , ServerResponse } from 'http' ;
75
8- export function resolve (
6+ export function createResponder (
97 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 ;
1713
18- if ( statusCode === 200 && req . method === 'POST' ) {
19- statusCode ++ ;
20- }
14+ if ( statusCode === 200 && req . method === 'POST' ) {
15+ statusCode ++ ;
16+ }
2117
22- res . statusCode = statusCode ;
23- stream . end ( normalized ) ;
24- } )
25- . pipe ( res ) ;
26- }
18+ res . statusCode = statusCode ;
2719
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