@mck-p/obvi

0.1.1 • Public • Published

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
  • _server: http.Server:
    • The underlying http.createServer() value
  • stop: () => http.Server:
    • How we stop listening for incoming requests
  • start: () => http.Server

Context

  • request: Request:
    • Our base abstraction over http.IncomingMessage
  • response: Response:
    • Our base abstraction over http.ServerResponse

Request

It is the underlying http.IncomingMessage with

  • url: Url:
    • Instead of string, it returns this object with true as the second argument.

Response

  • response: http.ServerResponse:
    • The underlying http.ServerResponse of the request
  • send: string => void:
    • An alias for response.end

Readme

Keywords

none

Package Sidebar

Install

npm i @mck-p/obvi

Weekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • beardedtim