Standard HTTP interfaces for HTTP clients and servers in node.js and browsers
npm install servie




> Standard, framework-agnostic HTTP interfaces for JavaScript servers and clients.
```
npm install servie --save
- throwback Compose middleware functions into a single function
- popsicle HTTP request library for node.js and browsers
- servie-lambda Servie transport layer for AWS Lambda
- servie-http Servie transport layer for node.js HTTP
- get-body General body parser for forms, JSON and text
- servie-cors CORS middleware for Servie
- servie-route Routing middleware for Servie
- servie-mount Mount Servie middleware on a path prefix
- servie-compat-http Mimic node.js HTTP using Servie
- servie-redirect Create response objects for redirection
- servie-cookie-store API for managing client-side cookies
- servie-errorhandler Standard error handler for transport layers
- servie-finalhandler Standard final handler for transport layers
- http-errors Create HTTP errors
- boom HTTP-friendly error objects
- consolidate Template rendering
`ts`
import { Body, Request, Response, Headers, AbortController } from "servie";
> Servie is a universal package, meaning node.js and browsers are supported without needing configuration. This means the primary endpoint requires some dom types in TypeScript. When in a node.js, or browser, only environment, prefer importing servie/dist/{node,browser} instead.
> Base HTTP class shared between Request and Response.
`ts`
new Body(body, headers);
#### Body
Supported body types, depending on environment:
- Node.js - string | Buffer | ArrayBuffer | Readable | null | undefinedstring | ArrayBuffer | ReadableStream | null | undefined
- Browser -
#### Properties and Methods
- bodyUsed Boolean whether the body is already consumedtext()
- Returns the body as a Promisejson()
- Returns the body with JSON.parse as PromisearrayBuffer()
- Returns the body as a Promiseclone()
- Clones an unconsumed bodydestroy()
- Consumes and destroys the body
> HTTP class for modelling a Request, extends Body.
`ts`
new Request(input [, init]);
#### Input
A URL string or another Request instance to clone from.
#### Init
- method? HTTP request method.body?
- Any support body types.signal?
- A signal from an AbortController.headers?
- A map, list of tuples, or Header instance to initialize from.trailer?
- A promise resolve to a support header initialization types (above).
#### Properties and Methods
- url Requested url stringmethod
- Requested method stringsignal
- Signal event emitterheaders
- A Headers instancetrailer
- A Promise instanceclone()
- Clones the request into a new instance
> HTTP class for modelling a Response, extends Body.
`ts`
new Response([body [, init]]);
#### Body
One of the support body types (above).
#### Init
- status? The numeric HTTP response status codestatusText?
- The HTTP response status text
#### Properties and Methods
- status The numeric HTTP response status codestatusText
- The HTTP response status textok
- Boolean indicates successful response (status between 200 and 299)headers
- A Headers instancetrailer
- A Promise instanceclone()
- Clones the response into a new instance
> Map representation of HTTP headers.
`ts`
new Headers([init]);
#### Init
Initialize headers from Iterable, a HeadersObject or an existing Headers instance.
#### Methods
- set(name: string, value: string | string[]): void Set a HTTP header by overriding case-insensitive headers of the same nameappend(name: string, value: string | string[]): void
- Append a HTTP headerget(name: string): string | undefined
- Retrieve a case-insensitive HTTP headergetAll(name: string): string[]
- Retrieve a list of matching case-insensitive HTTP headershas(name: string): boolean
- Check if a case-insensitive header is already setdelete(name: string): void
- Delete a case-insensitive headerasObject(): HeadersObject
- Return the lower-cased headers as a plain objectextend(obj: HeadersInit): this
- Extends the current headers with an objectkeys()
- Iterable of the available header namesvalues()
- Iterable of header valuesentries()
- Iterable of headers as [key, value]clear()
- Clears the headers instanceclone()
- Clones the Headers instance
> Simple controller for aborting a Request instance.
`ts`
new AbortController();
#### Properties and Methods
- signal A Signal instance to pass to a Requestabort()
- Used to abort any listening requests through the signal
> Tiny event emitter for communicating during a request.
#### Methods
- aborted Boolean indicating whether the request is abortedon(type, fn)
- Attach an event listener to an event typeoff(type, fn)
- Remove an event listener from an event typeeach(fn)
- Attach an event listener for all eventsnone(fn)
- Remove a global event listeneremit(type, ...args)
- Emit an event to all listeners
#### Standard Events
- abort The request has been abortedrequestBytes
- Emitted on request progress with current bytesrequestEnded
- The request has endedrequestStarted
- The request has been startedresponseBytes
- Emitted on response progress with current bytesresponseEnded
- The response has endedresponseStarted
- The response has started
Plugins can emit new types of events.
If you're building the transports for Servie, there are some life cycle events you need to be aware of:
1. Listen to the error event on signal for errorsabort
2. Listen to the event on signal to destroy the connectiontrailer
3. Resolve promise and append to HTTP request or responseSignalEvents` you can support
4. There are some existing built-in type-safe events in
This module is designed for ES2017 environments and published with TypeScript definitions on NPM.
Apache 2.0