@bakeryjs/router

1.0.1 • Public • Published

bakeryjs/router

Installation

Install library via npm:

$ npm install @bakeryjs/router

How to use

Create a new router:

const router = require('./router')();

// or (if it's needed to use different routers that will be isolated from each other)

const Router = require('./router');
const router = Router();

Configure route (endpoint) and it's handler:

router
  .handle('/authors/:authorId/books', createBook)
  .method('POST')
  .provideReqRes(true)
  .sendResponse(false);

Set handler for http server:

const server = http.createServer(router.handler);

API reference

Router:

  • (method) handle: (pattern: string, handler: function) - register endpoint and it's handler

NOTE: use ':' symbol to indicate a query parameter.

  • (property) handler: function - get http handler

Route:

  • (method) method(value: string) - set HTTP method for route (GET by default)
  • (method) provideReqRes(value: boolean) - provide req and res objects to handler function (false by default)
  • (method) sendResponse(value: boolean) - send response with function return value (true by default)

NOTE: if it's needed to send custom response from hanlder the provideReqRes should be true to get req object and use it to prepare and send response.

Demo

Take a look on demo project.

Package Sidebar

Install

npm i @bakeryjs/router

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

21.9 kB

Total Files

18

Last publish

Collaborators

  • yehorbk