install: npm i @ingress/core
A small abstraction around the http.Server class that uses promise-based middleware
import ingress, { DefaultMiddleware } from 'ingress'
const app = ingress(),
defaultMiddleware = new DefaultMiddleware({
onError: ({ error }) => console.log(error)
})
app.use(defaultMiddleware)
.use((context, next) => {
context.body = 'Hello World'
return next()
})
.listen(8080)
.then(() => console.log('Listening on port 8080'))
Ingress Core provides a DefaultMiddleware
export. The middleware contsructor accepts an options argument with an onError
handler which
is called whenever an error occurs during a request. Additionally, setting the context.body
property to one of
the following types String | Buffer | Stream | Object
will provide best guess headers and content length responses.
- ServerOptions<T> Object Interface:
- server?: http.Server
- provide an existing http server
- contextFactory?: <T>({ req: IncomingMessage, res: ServerResponse}) => T
- provide an alternate factory function for per-request context creation
- server?: http.Server
- Underlying http server implementation
- The listen method takes the same arguments as the
net.Server.prototype.listen
method except it returns a promise, in lieu of accepting a callback.
- Usable<T> Function & Object Interface:
- (context: T, next: () => Promise<void>): any
- register?(server: Server<T>): Promise<void>
- Optionally register a server addon. The resulting promise is waited upon, and resolved before binding the server to a port after
listen
is called
- Optionally register a server addon. The resulting promise is waited upon, and resolved before binding the server to a port after
- middleware: Middleware<T>
- de-referencable middleware function property
- Shudown the server.
- get a callback made up of all middleware, suitble for use as a server request handler
- Implemented with: Context (BaseContext<Context>) - factory: createContext,
- IncomingMessage & { context: T }
- ServerResponse & { context: T }
- Response body content
Ingress has the following exports
- Ingress<T> (alias: Server, factory: default export)
- Context
- createContext
- DefaultMiddleware<T>
Additionally, the necessary TypeScript interfaces are exposed.