esrol-router

1.0.0 • Public • Published

NPM version Build Status Test coverage

esrol-router

You can create new routes, set allowed ways of reaching them, handle request, get information regarding those routes, and set middleware.

Part of Esrol

Installation

$ npm install --save esrol-router

Usage

'use strict';
const Router = require('esrol-router');
const http = require('http');
const router = new Router();
const route = {
  url: '/posts',
  getMultipleRecords: function(req, res) {
    return res.end('posts');
  },
  getSingleRecord: function(req, res) {
    return res.end(req.record);
  }
};
router.setSupportedHttpMethods(['GET']);
router.registerRoute(route);
 
http.createServer((req, res) => {
  router.onRequest(req, res);
}).listen(3333);
 
//curl localhost:3333/posts
//curl localhost:3333/posts/1

Please see the docs here, for information how to structure your route

Methods

registerRoute(route)boolean

Register a route.

getRoutesLength()int

Get the ammount of registered routes.

getRouteMethodsLength(url)int

Get the methods number in the route.

setSupportedHttpMethods(methods)boolean

Set the supported http methods.

setNamespace(namespace)boolean

Set server namespace.

setMiddleware(middleware)boolean

Initialise middlewares.

onRequest(req, res)object

Handle a request and route it to the required route.

registerRoute(route) ⇒ boolean

Register a route.

Returns: boolean - true - returns true if registering was successful
Throws:

  • error - throws error if thrown by registerRoute in Routes
Param Type Description
route object a route object, containing intormation for the route

getRoutesLength() ⇒ int

Get the ammount of registered routes.

Returns: int - - ammount of registered routes

getRouteMethodsLength(url) ⇒ int

Get the methods number in the route.

Returns: int - 0 or integer - returns 0 if there are no set routes, or an integer number

Param Type Description
url string path to the route

setSupportedHttpMethods(methods) ⇒ boolean

Set the supported http methods.

Returns: boolean - true - returns true on success
Throws:

  • error error - if thrown by setSupportedHttpMethods
Param Type Description
methods array allowed http methods

setNamespace(namespace) ⇒ boolean

Set server namespace eg 'v1' which will evaluate www.example.com/v1

Returns: boolean - true

Param Type Description
namespace string eg /v1

setMiddleware(middleware) ⇒ boolean

Initialise middlewares.

Returns: boolean - true
Throws:

  • error error - if thrown by setMiddleware
Param Type Description
middleware function has to have 4 parameters

onRequest(req, res) ⇒ object

Handle a request and route it to the required router.

Returns: mixed - mixed - returns the value from the route // if returned

Param Type Description
req object request
res object response

Contriubtion

Any contribution will be highly appreciated. Just make sure that:

  1. Your code works.
  2. You have 100% successful tests coverage.
  3. You have comments in your code.
  4. Follows eslint config. Exceptions are possible where that make sense.

Tests

To run the test suite, first install the dependencies, then run npm test:

$ npm install
$ npm test

License

MIT

Package Sidebar

Install

npm i esrol-router

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • ivaylopivanov