README
polkadot-router
See also: koa-bestest-router
Built for the tiny, nifty polkadot server framework.
No mutations, not much code = easy to understand and debug.
API
A single function. It takes a POJO map of HTTP methods to a map of routes to handler functions.
It returns a request handler function.
const createRouter = require('polkadot-router')
const notFoundHandler = (req, res) => {
res.statusCode = 404
return '404 not found sorry'
}
const router = createRouter({
'/pie': {
GET: async (req, res) => {
return 'Yay pie!'
}
},
'/cake/:flavor': {
GET: async (req, res) => {
return `I like ${req.params.flavor} cake`
},
POST: async (req, res) => {
await someDb.addFlavor(req.params.flavor)
}
}
}, notFoundHandler)
polkaDot(router).listen(8080)
router = createRouter(routes, [notFoundHandler])
routes
is a map of HTTP methods to maps of @tehshrike/regexparam routes to handler functions.
Routes will be checked in a deterministic order from top to bottom, thanks to ES2015.
notFoundHandler
is the function to be called if no route is found. Defaults to:
(req, res) => {
res.statusCode = 404
return `404 not found`
}