@mck-p/obvi
v0.1.1
Published
An HTTP server using Observables
Downloads
3
Readme
Observable Server
Overview
@mck-p/obvi
is a simple wrapper around http.createServer
. It exposes a few helper methods and wraps each request inside of a Context
object. The goal of this library is to be the lowest abstraction for more robust userland libraries.
Usage
const makeServer = require('@mck-p/obvi')
const {
_server, // http.creatServer() response
requests, // Observable of requests sent to server
start, // Start server listening on passed in port
stop // Stop server
} = makeServer({
port: 5000 // Port to listen for connections on
})
// We subscribe to all requests from this server
requests.subscribe((context) => {
console.log('I am responding to a request!')
})
// We create a stream of all post requests
const postRequests = requests.filter(({ request }) => request.method.toLowerCase() === 'post')
postRequests
// And we handle all post requests
.subscribe(({ response }) => response.send('You are sending me a post request!'))
API
makeServer({ port?: number })
:- Main export of module
- Returns a
Server
object
Server
requests: Observable<Context>
:- The main abstraction over
http
requests
- The main abstraction over
_server: http.Server
:- The underlying
http.createServer()
value
- The underlying
stop: () => http.Server
:- How we stop listening for incoming requests
start: () => http.Server
Context
request: Request
:- Our base abstraction over
http.IncomingMessage
- Our base abstraction over
response: Response
:- Our base abstraction over
http.ServerResponse
- Our base abstraction over
Request
It is the underlying http.IncomingMessage
with
url: Url
:- Instead of
string
, it returns this object withtrue
as the second argument.
- Instead of
Response
response: http.ServerResponse
:- The underlying
http.ServerResponse
of the request
- The underlying
send: string => void
:- An alias for
response.end
- An alias for