micro-fork
A fast and functional router for ZEIT's Micro. Inspired by microrouter, use find-my-way underneath.
Features
- Tiny. Just couple lines of code.
- Functional. Write your http methods using functions.
- Async. Design to use with
async/await
Usage
Install as project dependency:
$ npm install micro-fork
Then you can define your routes inside your microservice:
const send = const router get = const hello = const notfound = moduleexports =
async/await
You can use your handler as an async function:
const send = const router get = const hello = async moduleexports =
router
Initialize a router:
routeMethodA routeMethodB // ...
The options
will directly goes to find-my-way
route methods
Each route is a single basic http method that you import from micro-fork
and has the same arguments:
get(path = String, handler = Function, [store = Object])
post(path = String, handler = Function, [store = Object])
put(path = String, handler = Function, [store = Object])
patch(path = String, handler = Function, [store = Object])
del(path = String, handler = Function, [store = Object])
head(path = String, handler = Function, [store = Object])
options(path = String, handler = Function, [store = Object])
path
A simple url pattern that you can define your path. In this path you can set your parameters using a :
notation. The req
parameter from handler
will return this parameters as an object.
For more information about how you can define your path, see find-my-way that's the package that we're using to match paths.
handler
The handler
method is a simple function that will make some action base on your path.
The format of this function is (req, res, store) => {}
req.params
As you can see below, the req.params
parameter represents the parameters defined in your path
:
// service.jsconst send = const router get = moduleexports = // test.jsconst request = const response = await console // { who: 'World' }
req.query
req.query
represents parsed query parameters:
// service.jsconst send = const router get = moduleexports = // test.jsconst request = const response = await console // { from: 'john' }
store
Last argument, store
is used to pass an object that you can access later inside the handler function. If needed, store can be updated.
Parsing Body
By default, router doens't parse anything from your requisition, it's just match your paths and execute a specific handler. So, if you want to parse your body requisition you can do something like that:
// service.jsconst router post = const json send = const user = async { const body = await } moduleexports = // test.jsconst request = const body = id: 1 const response = await request
License
ISC @ Amio