Package Exports
- servie
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (servie) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Servie provides standard, framework-agnostic HTTP interfaces for servers and clients.
Installation
npm install servie --saveUsage
throwbackCompose middleware functions into a single functionservie-lambdaServie transport layer for AWS LambdabusboyA streaming parser for HTML form dataqsandquerystringParse the HTTP query string to an objectconsolidateTemplate renderingget-bodyGeneral body parser for forms, JSON and text
Common
Base HTTP class for common request and response logic.
import { Common } from 'servie'Options
headers?HTTP headers (Headers | HeadersObject | string[])trailers?HTTP trailers (Headers | HeadersObject | string[])events?An event emitter object (EventEmitter)body?Allowed HTTP bodies (string | Buffer | Readable | object)
Properties
eventsAn event emitter for listening to the request and response lifecyclesheadersThe headers as aHeadersinstancetrailersThe trailers as aHeadersinstancebodyThe request or response payloadbodyUsedA boolean indicating whether the body has been readtypeA shorthand property for reading and writing theContent-TypeheaderlengthA shorthand property for reading and writingContent-Lengthas a numberstartedBoolean indicating if a request/response has startedfinishedBoolean indicating if a request/response has finishedbytesTransferredThe number of bytes sent in the HTTP request/response
Methods
buffer(maxBufferSize): Promise<Buffer>Read the body into aBufferobjecttext(maxBufferSize): Promise<string>Read the body as astringstream(): ReadableRead the body as aReadablestream
Events
headersEmitted when theheadersobject is availabletrailersEmitted when thetrailersobject is availablestartedEmitted whenstarted === truefinishedEmitted whenfinished === trueprogressEmitted whenbytesTransferredis incremented
Request
HTTP class for encapsulating a
Request, extendsCommon.
import { Request } from 'servie'Options
const request = new Request({
url: '/',
method: 'GET'
})Extends
Commonoptions.
urlThe HTTP request url (string)method?The HTTP request method (string, default:GET)connection?Connection information ({ remoteAddress?: string, remotePort?: number, localAddress?: string, localPort?: number, encrypted?: boolean })
Properties
urlThe HTTP request url (string)methodThe HTTP request method upper-cased (string)UrlThe HTTP request url as a read-only parsed object (object)connectionConnection information ({})
Methods
abort(): booleanEmit an abort eventerror(message, code, status?, original?): HttpErrorCreate a HTTP error instance
Events
abortEmitted when the request is aborted and MUST be handled by transporterrorEmitted when an out-of-band error occurs (e.g. abort) and MUST be handled by the transportresponseEmitted when the response object is being handled
Response
HTTP class for encapsulating a
Response, extendsCommon.
import { Response } from 'servie'Options
const response = new Response({})Extends
Commonoptions.
status?The HTTP response status code (number)statusText?The HTTP response status message (string)
Properties
status?The HTTP response status code (number)statusText?The HTTP response status message (string)
Headers
Used by
CommonforRequestandResponseobjects.
Options
Take a single parameter with the headers in object, array or Headers format.
Properties
rawThe raw HTTP headers list (string[])
Methods
object(obj?: HeadersObject | null): HeadersObject | voidA getter/setter method for reading the headers as a lower-cased object (like node.js)set(name: string, value: string | string[]): thisSet a HTTP header by overriding case-insensitive headers of the same nameappend(name: string, value: string | string[]): thisAppend a HTTP headerget(name: string): string | undefinedRetrieve a case-insensitive HTTP headergetAll(name: string): string[]Retrieve a list of matching case-insensitive HTTP headershas(name: string): booleanCheck if a case-insensitive header is already setdelete(name: string): thisDelete a case-insensitive header
HttpError
Internally and externally triggered HTTP errors.
Properties
codeA unique error code (string)statusA HTTP status code (number)requestTheRequestinstance that triggered the error (Request)messageStandard error message (string)causeSpecified when the HTTP error was triggered by an underlying error
Implementers
If you're building the transports for Servie, there are some life cycle events you need to be aware of and emit yourself:
- Listen to the
errorevent onRequestfor out-of-band errors and respond accordingly (e.g. app-level logging) - Listen to the
abortevent onRequestto destroy the HTTP request - Set
started === trueandfinished === trueonRequestandResponse, as appropriate - Set
bytesTransferredonRequestandResponsewhen monitoring HTTP transfer progress
JavaScript
This module is designed for ES5 environments, but also requires Promise to be available.
TypeScript
This project is written using TypeScript and publishes the definitions directly to NPM.
License
MIT