0.1.1 • Published 6 years ago

@mck-p/obvi v0.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

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
0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago