1.0.0 • Published 7 years ago

esrol-router v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

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.

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

  • error - throws error if thrown by registerRoute in Routes
ParamTypeDescription
routeobjecta 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

ParamTypeDescription
urlstringpath 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
ParamTypeDescription
methodsarrayallowed http methods

setNamespace(namespace) ⇒ boolean

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

Returns: boolean - true

ParamTypeDescription
namespacestringeg /v1

setMiddleware(middleware) ⇒ boolean

Initialise middlewares.

Returns: boolean - true
Throws:

  • error error - if thrown by setMiddleware
ParamTypeDescription
middlewarefunctionhas 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

ParamTypeDescription
reqobjectrequest
resobjectresponse

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

1.0.0

7 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago