https://github.com/plaidev/router-fns
Absolutely Simple Routing Library - All you need is handler
https://github.com/plaidev/router-fns
handler nodejs router routing simplex
Last synced: 3 months ago
JSON representation
Absolutely Simple Routing Library - All you need is handler
- Host: GitHub
- URL: https://github.com/plaidev/router-fns
- Owner: plaidev
- License: mit
- Created: 2019-04-12T07:49:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-08T13:10:06.000Z (about 3 years ago)
- Last Synced: 2026-01-12T02:28:27.475Z (6 months ago)
- Topics: handler, nodejs, router, routing, simplex
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/router-fns
- Size: 1.36 MB
- Stars: 0
- Watchers: 8
- Forks: 0
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# router-fns : absolutely simple routing libary
Absolutely simple, so that strong. All you need is handler, as with linux's process. It's just that.
## Installation
```
$ npm i -SE router-fns
```
## Usage
### Handler
Express-like handler.
```ts
const handler = (req, res, next) => {
try {
// do something
next();
} catch (e) {
next(e);
}
};
```
### reduceHandlers
Reduce multiple handlers into one handler.
This reducer is designed to implement middleware pattern.
```ts
import { reduceHandlers } from 'router-fns';
const handler = reduceHandlers(handlerA, handlerB, handlerC);
```
### initHandler
A handler which initializes request object for routing. It is required to be called before routing.
```ts
import { reduceHandlers, initHandler } from 'router-fns';
const rootHandler = reduceHandlers(initHandler, router);
```
### routingHandler
Create a handler which aims to route recieved request to appropriate handler.
```ts
import { routingHandler, initHandler } from 'router-fns';
const router = routingHandler({
'/routeA': routingHandler({
'/subRouteA': handlerA
}),
'/routeB': handlerB
});
const rootHandler = reduceHandlers(initHandler, router);
rootHandler({ url: '/routeA/subRouteA' }, {}, e => {
if (e) throw e;
});
```