Add section to README on server

This commit is contained in:
Starbeamrainbowlabs 2019-04-27 17:41:45 +01:00
parent a1e62c11e4
commit 46fa45600a
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
2 changed files with 18 additions and 7 deletions

View File

@ -9,8 +9,6 @@ _Powahroot_ is a pair of micro routing frameworks, presented as an ES6 module:
It's based on [`rill`](https://www.npmjs.com/package/rill) (see the npm package bearing the name), but stripped down and simplified.
**Documentation is work-in-progress!**
## Getting Started
Install powahroot as a dependency with npm:
@ -41,7 +39,6 @@ Syntax | Meaning
`/add/vegetable/:name/:weight` | Parameters. Match values an pull them into an object automatically. Does not like forward slashes in parameter values.
`/images/::path` | Parameter values with forward slashes. If you want to use parameters, but need values to be able to contain forward slashes `/`, this is for you. Don't forget you can mix-and-match this with the previous example!
### Client
Initialise a new router like this:
@ -54,7 +51,16 @@ const router = new ClientRouter({
```
### Server
TODO: Finish this section
The server router works slightly differently, to account for the different environment it's designed for. Here's how to use it:
```js
const router = new ServerRouter();
router.on_all(async (context, next) => { console.debug(context.url); await next()})
router.get("/files/::filepath", (context, _next) => context.send.plain(200, `You requested ${context.params.filepath}`));
// .....
```
The `context` argument there is of type `RouterContext`. Check out the API reference (link below) to learn about the other useful properties it has.
### Reference
TODO: Generate reference from code automatically

View File

@ -12,7 +12,11 @@ import { pathspec_to_regex } from '../Shared/Pathspec.mjs';
class ServerRouter
{
constructor(verbose = false) {
/** The actions to run in turn. */
/**
* The actions to run in turn.
* @private
* @type {Array<Function>}
*/
this.actions = [];
/** Whether to activate versbose mode. Useful for debugging the router. */
this.verbose = verbose;
@ -75,10 +79,10 @@ class ServerRouter
/**
* Execute the specified action for requests matching the given parameters.
* TODO: Consider merging with on_all and refactoring to take an object literal which we cna destructure instead
* TODO: Consider merging with on_all and refactoring to take an object literal which we can destructure instead
* @param {array} methods The HTTP methods to run the action for. Include '*' to specify all methods.
* @param {string|regex} pathspec The specification of the paths that this action should match against. May be a regular expression.
* @param {Function} action The action to execute. Will be passed the parameters `context` (Object) and `next` (Function).
* @param {Function} action The action to execute. Will be passed the parameters `context` (RouterContext) and `next` (Function).
*/
on(methods, pathspec, action) {
let regex_info = pathspec instanceof RegExp ? {regex: pathspec, tokens: [] } : pathspec_to_regex(pathspec);
@ -171,6 +175,7 @@ class ServerRouter
/**
* Iterates over all the generated middleware.
* @return {Generator} A generator that returns each successive piece of middleware in turn.
* @generator
*/
*iterate() {
for(let action of this.actions) {