Obvi
An HTTP server based on Observables
Usage
To see an example server running, install the repo ( yarn install
), and then run the server ( yarn dev
). This will start the server at the example.ts
file.
Below is a stripped down version of using ohz
with a vanilla server:
const router json server: createServer = const server = server
Below is a vanilla JavaScript usage example with detailed comments:
// We are going to be using the map operator// to make our middleware easier to writeconst of } = const map = // We also want to import some middleware (router, json), // along with our server creatorconst router json server: createServer = // We create a server instance// It has some basic generics that// can get us up and running quickly// so if you just want to see how it works// you can do//// server = createServer()// // without passing a configuration objectconst server = server // Just like connect, we can call `use` to add middleware // to our server instance. They take a different signature // though! Instead of `(ctx, next) => void` or // `(req, res, next)` => void, // this middleware is of type // (ctx) => Observable<Context>. // // We have included some basic middlewares in order to // get some basic boilerplate taken care of. `json` is // a module that takes in nothing and returns middleware // that handles incoming JSON data along with outgoing // // It does assume that the end-user is calling `send` // added by this middleware. If you change your subscribeFn // when you `listen`, you might get different results. // We also have a way to add routes based on express-like urls // router.route takes in an express-like url and a handler. The // handler can either return a new Context value or return an // Observable // // router.route will handle _all_ methods to that route. // If you want to only respond to get requests, // we can use router.get, which also takes a // route and handler but will only respond // to get requests to that endpoint // Just like express, we can use named parameters // in our routes. This will be called when we have // the url /posts/1234. // // As you can see in the body of this handler, we // have access to the params ( such as id ), along // with they query ( ) // We can also add generic Pipe-able functions // by using rxjs/operators. map(fn) is the same // as writing a function of Obs => Obs.map(fn) // Once we call listen, we can no longer add/remove middleware // // We give listen a port, callback function, and subscribe function // // port is the port to listen on // callback is the function to be called when the server is // actually listening on that port // subscribe is a function that takes a Context and responds // to the eventual value