1.1.1 • Published 4 months ago

fsbr v1.1.1

Weekly downloads
4
License
MIT
Repository
github
Last release
4 months ago

npm version types size coverage Dependencies devDependencies License

file structure based router for servers

Installation

$ npm i fsbr

Usage

Native

import router from 'fsbr';
import {createServer} from 'http';

const {use, register, route} = router();

use((req, res, next) => {
    //
    next();
});

register('./routes');

const server = createServer(route);
server.listen(8080);

Express

import router from 'fsbr';
import express from 'expess';

const app = express();
const {use, register, route} = router();

use((req, res, next) => {
    //
    next();
});
register('./routes');

app.use(route);
app.listen(3000);

API

  • fsbr(config)
  • router.on()
  • router.has()
  • router.use()
  • router.chain()
  • router.route()
  • router.register()

Library

fsbr(config: Config)

Config

defaultdescription
ext.jsextension of middleware and listener files
entryindexname of middleware files e.g. middleware
devfalseprint errors in final listener

Creates a new fsbr instance.

import router from 'fsbr';

const {on, use, chain, register, route} = router();

router.on(method, path, listener)

Registers a route. A Method can be any known HTTP method/verb or a wildcard *. Paths can contain a variable denoted via a semicolon. In this case, listeners receive a third optional argument with the resolved variable. Paths can also have a wildcard. fsbr will match every request after that.

const {on} = router();

// plain route
on('POST', '/post', (req, res) => {
    //
});

// route with id parameter
on('GET', '/user/:id', (req, res, params) => {
    const {id} = params;
});

// route with wildcard method
// any request with any HTTP method/verb on this route executes the listener
on('*', '/foo', (req, res) => {
    //
});

// route with wildcard in pathname
// any request with '/proxy/...' executes the listener
on('GET', '/proxy/*', (req, res) => {
    //
});

router.has(method, path)

Returns true, if the route exists.

const {has} = router();

has('POST', '/post');

router.use(middleware)

Registers a middleware function to the router. Middlewares with 4 parameters are considered as error listeners. Note the order. The error parameter here should be on the fourth place, unlike in other frameworks like express.

const {use} = router();

// normal middleware
use('POST', '/post', (req , res, next) => {
    // do middleware things
    next();
});

// middleware for errorhandling
// notice the fourth and last argument "error"
use('get', '/photos', (req, res, next, error) => {
    // handle error
    console.error(error);
    next();
});

router.chain(...middlewares)

Transforms an array of middleware functions into a single middleware function.

const {chain, on} = router();

const middlewares = [
    (req, res, next) => {
        res.data = [];
        next();
    },
    (req, res, next) => {
        res.data.push('foobar');
        next();
    },
];

on('GET', '/custom', chain(...middlewares, (req, res) => {
    console.log(res.data); // ['foobar']
}));

router.route(req, res)

Handle the incoming requests.

const {route} = router();

const server = createServer((req, res) => {
    route(req, res);
});
server.listen(8080);

router.register(base, cb)

Recursively register all routes within the base folder and call the optional callback when finished. Each directory represents a part of the URL pathname. Dynamic routes can be created by enclosing the directory name in square brackets e.g. [id]. Listeners are named by HTTP methods/verbs and export a default listening function. Middlewares can be placed alongside the listeners as index files. These files can export a single middleware function or an array of functions.

const {register} = router();

register(__dirname + '/routes', () => {
    console.log('done');
});

Example

Registering the following folder/file structure with default configuration:

routes
|
├───api
|       index.js
|       get.js
|
├───photo
|   |   index.js
│   │   get.js
│   │   post.js
│   │   delete.js
│   │
│   ├───[id]
│   │       get.js
│   │
│   └───vacation
|           index.js
│           get.js
│           post.js
│
└───user
        post.js

Would bind the following routes:

  • GET: example.com/api
  • GET: example.com/photo
  • POST: example.com/photo
  • DELETE: example.com/photo
  • GET: example.com/photo/:id
  • GET: example.com/photo/vacation
  • POST: example.com/photo/vacation
  • POST: example.com/user

A GET call to /photo/vacation would execute the following files in order:

  • photo/index.js
  • photo/vacation/index.js
  • photo/vacation/get.js

Licence

MIT License, see LICENSE

1.1.1

4 months ago

1.1.0

6 months ago

1.0.11

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.10

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago