yaf-router
TypeScript icon, indicating that this package has built-in type declarations

0.3.5 • Public • Published

Yet Another F@#%ing Router

npmnpm type definitionsSnyk Vulnerabilities for npm packagenpmlast commit

A basic HTTP-Router with zero dependencies. Made for NodeJS-HTTP(s)-Server but the route-handling only depends on request.url and request.method so it should work with anything. The Middlewares require response.write(), response.end() and response.writableEnded.

Getting started

const NodeHttp = require('http');
const {Router} = require('yaf-router')

const router = new Router()

// GET /bestUser/foobar => "Hello bestUser, foobar"
router.addRoute('/{user}/{msg}', ({parameter, response}) => {
  response.write(`Hello ${parameter.user}, ${parameter.msg}`)
  response.end()
})

const server = NodeHttp.createServer((request, response) => {
  router.handle(request, response)
});
server.listen(8080);

Documentation

Routes

Middlewares

Interfaces

HandlerData

The HandlerData stores the Parameter, Request, Response and almost always the written data.

interface HandlerData {
    parameter: {[key: string]: string},
    request: NodeHttp.IncomingMessage,
    response: NodeHttp.ServerResponse,
    written?: Array<any>
}

Routes

A Route consists of a Method and URL: GET /hallo/User. The Method is optional and defaults to GET.

router.addRoute('/me', ({parameter, response}) => {
	// ...
  response.end()
})
// is the same as 
router.addRoute('GET /me', ({parameter, response}) => {
	// ...
  response.end()
})

You can define multiple Routes for the same Handler by passing an Array of Routes

router.addRoute(['/me', 'POST /you'], ({parameter, response}) => {
	// handles: GET /me and POST /you
  response.end()
})

Parameter

Parameter are part of the URL like /search/{query} .

// GET /search/foobar => "Searching: foobar"
router.addRoute('/search/{query}', ({parameter, response}) => {
  response.write(`Searching ${parameter.query}`)
  response.end()
})

Order and count of Parameter do not matter. There may be multiple Paramater in a row.

// GET /user/bestUser/foobar => "Hello bestUser, foobar"
router.addRoute('/user/{user}/{msg}', ({parameter, response}) => {
  response.write(`Hello ${parameter.user}, ${parameter.msg}`)
  response.end()
})

Wildcard-Parameter are defined by the URI-Part /* and have to come last. Wildcard-Parameter are accessed by the *-Key. Normal Parameter are prioritized and matched first.

// GET /wildcard/BestUser/foo/bar => "BestUser is trying to access foo/bar"
router.addRoute('/wildcard/{user}/*', ({parameter, response}) => {
  response.write(`${parameter.user} is trying to access ${parameter['*']}`)
  response.end()
})

If there are multiple Parameter with the same key in a URL only the last value will be used. In the future this case may be handled as an Array.

// GET /search/foo/spacing/bar => "Searching: bar"
router.addRoute('/search/{search}/spacing/{search}', ({parameter, response}) => {
  response.write(`Searching: ${parameter.search}`)
  response.end()
})

Middleware

A Middleware implements the MiddlewareInterface. Middlewares are called after routing so it is not possible to reroute via Middlewares.

class CorsMiddleware implements MiddlewareInterface {
  getTypes() {
    return [
      MiddlewareOrderType.Header
    ]
  }
  
  getName() {
    return 'CorsMiddleware'
  }
  
  async handleByType(type: MiddlewareOrderType, handlerData: HandlerData, path: string = '') {
    if(type !== MiddlewareOrderType.Header) {
      return
    }
    handlerData.response.setHeader('Access-Control-Allow-Origin', '*');
		handlerData.response.setHeader('Access-Control-Request-Method', '*');
		handlerData.response.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
		handlerData.response.setHeader('Access-Control-Allow-Headers', '*');
  }
}

The order of Middleware execution and their suggested usage is:

  1. Security: Makes security checks and ends the response if needed
  2. Header: Only adds header to the response
  3. Data: Enriches the HandlerData with additional information
  4. Result: Manipulates the Response
  5. Finisher: Can be used for caching for example

Every Handler can end the Response but only expected should do so.

Included Middlewares

Cache

A simple Cache implementation. Uses MiddlewareOrderType.Data if cache hit and MiddlewareOrderType.Finisher to fill cache. Default Cache-TTL is 60 seconds. Only caches GET, OPTIONS and HEAD methods.

Cors

A simple Middleware that adds CORS-Header.

Readme

Keywords

Package Sidebar

Install

npm i yaf-router

Weekly Downloads

1

Version

0.3.5

License

ISC

Unpacked Size

32.6 kB

Total Files

19

Last publish

Collaborators

  • sjs-one