- HTTPError
Represent a HTTP error with status code and message.
- RequestBody
Represent the body (payload or data) of a HTTP request. You can handle it via the stream field or fetch and use it as buffer, text or json via the provided helpers
- Request
Represent a HTTP request.
- Response
Represent a HTTP response. By default, a response is just like a plain object with some default values. You can create a response and set it on your own. However, it's usually convenient and better to use the static builders to build a proper initial response and make any needed adjustment to it.
- adapt(handler) ⇒
NodeHTTPHandler
Convert a TH handler into a node HTTP handler.
- buildDownwardWrapper(downwardHandler) ⇒
THWrapper
Build a wrapper which could handle the downward logic. Generally it maps request (context), but if it returns a response, the flow will return right here, and the handlers below will be ignored.
- buildErrorWrapper(errorHandler) ⇒
THWrapper
Build a wrapper which could handle the error logic.
- buildUpwardWrapper(upwardHandler) ⇒
THWrapper
Build a wrapper which could handle the upward logic. Generally it maps response.
- compose(...wrappers) ⇒
function
Compose wrappers
- listen(...args) ⇒
function
Create a http server and listen for connections.
- handleErrors([options]) ⇒
THWrapper
Provided default error handler wrapper. Generate response from the error and return it. If it's not a http client error, then log it.
- THHandler :
function
- NodeHTTPHandler :
function
- THWrapper :
function
Represent a HTTP error with status code and message.
Param | Type |
---|---|
[statusCode] | number |
[message] | string |
[originalError] | Error |
Represent the body (payload or data) of a HTTP request. You can handle it via the stream field or fetch and use it as buffer, text or json via the provided helpers
Kind: global class
- RequestBody
- .stream :
ReadableStream
- .asBuffer([options]) ⇒
Promise.<Buffer>
- .asText([options]) ⇒
Promise.<string>
- .asJSON([options]) ⇒
Promise.<*>
- .stream :
Kind: instance property of RequestBody
Get the body as buffer.
Kind: instance method of RequestBody
Param | Type | Default |
---|---|---|
[options] | Object |
|
[options.limit] | string |
"1mb" |
Get the body as string.
Kind: instance method of RequestBody
Param | Type | Default |
---|---|---|
[options] | Object |
|
[options.limit] | string |
"1mb" |
Get the body as JSON object.
Kind: instance method of RequestBody
Param | Type | Default |
---|---|---|
[options] | Object |
|
[options.limit] | string |
"1mb" |
Represent a HTTP request.
Kind: global class
- Request
- .httpVersion :
string
- .method :
string
- .url :
string
- .query :
string
- .headers :
Object
- .body :
RequestBody
- .parsedUrl :
Object
- .parsedQuery :
Object
- .httpVersion :
Kind: instance property of Request
Kind: instance property of Request
Kind: instance property of Request
Kind: instance property of Request
Kind: instance property of Request
See: headers
request.body : RequestBody
Kind: instance property of Request
Url object parsed by url.
Kind: instance property of Request
Query object parsed by querystring.
Kind: instance property of Request
Represent a HTTP response. By default, a response is just like a plain object with some default values. You can create a response and set it on your own. However, it's usually convenient and better to use the static builders to build a proper initial response and make any needed adjustment to it.
Kind: global class
- Response
- new Response()
- instance
- .statusCode :
number
- .statusCode :
number
- .headers :
Object
- .headers :
Object
- .body :
string
|Buffer
|ReadableStream
- .body :
string
|Buffer
|ReadableStream
- .setHeader(name, value)
- .getHeader(name) ⇒
string
|Array.<string>
- .statusCode :
- static
Create a new response and set the default fields
Status code of this response. The range is [100, 600)
Kind: instance property of Response
Kind: instance property of Response
Headers of this response.
Generally you should use setHeader
to avoid duplicate headers,
but if you know what you are doing, you can use this setter to update headers more efficiently.
Kind: instance property of Response
Kind: instance property of Response
Body of this response.
Kind: instance property of Response
Kind: instance property of Response
Set the header value of given header name. The name will be converted to lowercase to avoid duplication.
Kind: instance method of Response
Param | Type |
---|---|
name | string |
value | string | Array.<string> |
Return the header value of given header name.
Kind: instance method of Response
Param | Type |
---|---|
name | string |
Response.withStatusCode(statusCode, [message]) ⇒ Response
Build a response with given status code. The body will be set as the corresponding status text if not provided
Kind: static method of Response
Param | Type |
---|---|
statusCode | number |
[message] | string |
Response.withBufferBody(bufferBody) ⇒ Response
Build a response with given buffer body. The content-type header will be set as 'application/octet-stream'.
Kind: static method of Response
Param | Type |
---|---|
bufferBody | Buffer |
Response.withStreamBody(streamBody) ⇒ Response
Build a response with given stream body. The content-type header will be set as 'application/octet-stream'.
Kind: static method of Response
Returns: Response
- response
Param | Type |
---|---|
streamBody | ReadableStream |
Response.withTextBody(textBody) ⇒ Response
Build a response with given text body. The body could be of any type, but it will be converted into string. The content-type header will be set as 'text/plain; charset=utf-8'.
Kind: static method of Response
Returns: Response
- response
Param |
---|
textBody |
Response.withJSONBody(jsonBody) ⇒ Response
Build a response with given JSON body. The body could be of any type, but it will be converted into JSON string. The content-type header will be set as 'application/json; charset=utf-8'.
Kind: static method of Response
Returns: Response
- response
Param |
---|
jsonBody |
adapt(handler) ⇒ NodeHTTPHandler
Convert a TH handler into a node HTTP handler.
Kind: global function
Param | Type |
---|---|
handler | THHandler |
buildDownwardWrapper(downwardHandler) ⇒ THWrapper
Build a wrapper which could handle the downward logic. Generally it maps request (context), but if it returns a response, the flow will return right here, and the handlers below will be ignored.
Kind: global function
Param | Type |
---|---|
downwardHandler | DownwardHandler |
Kind: inner typedef of buildDownwardWrapper
Prototype: async (request: Request) => Request|Response
See
buildErrorWrapper(errorHandler) ⇒ THWrapper
Build a wrapper which could handle the error logic.
Kind: global function
Param | Type |
---|---|
errorHandler | ErrorHandler |
Kind: inner typedef of buildErrorWrapper
Prototype: async (error: Error, request: Request) => Response
See
buildUpwardWrapper(upwardHandler) ⇒ THWrapper
Build a wrapper which could handle the upward logic. Generally it maps response.
Kind: global function
Param | Type |
---|---|
upwardHandler | UpwardHandler |
Kind: inner typedef of buildUpwardWrapper
Prototype: async (response: Response, request: Request) => Response
See
Compose wrappers
Kind: global function
Param | Type |
---|---|
...wrappers | function |
Create a http server and listen for connections.
Kind: global function
Param | Description |
---|---|
...args | the same with that of server.listen of node http package |
handleErrors([options]) ⇒ THWrapper
Provided default error handler wrapper. Generate response from the error and return it. If it's not a http client error, then log it.
Kind: global function
Param | Type | Default |
---|---|---|
[options] | Object |
|
[options.logError] | function |
console.error |
Kind: global typedef
Prototype: async (request: Request) => Response
See
Kind: global typedef
Prototype: (req, res) =>
See: Node.js create server
Kind: global typedef
Prototype: (handler: THHandler) => THHandler
See: THHandler