https://github.com/Synvox/router
A tiny routing library to complement micro inspired by the good and bad parts of express.js.
https://github.com/Synvox/router
Last synced: 6 months ago
JSON representation
A tiny routing library to complement micro inspired by the good and bad parts of express.js.
- Host: GitHub
- URL: https://github.com/Synvox/router
- Owner: Synvox
- License: mit
- Archived: true
- Created: 2019-04-20T01:38:06.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-02-28T15:21:38.000Z (over 2 years ago)
- Last Synced: 2025-04-19T00:14:10.193Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 1.03 MB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 19
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- awesome-micro - @synvox/router - A tiny routing library inspired by react hooks and express.js. (Modules / Routing)
README
# `@synvox/router`
[](https://travis-ci.org/Synvox/router)
[](https://codecov.io/gh/Synvox/router)
A tiny routing library to complement [micro](https://github.com/zeit/micro) inspired by the good and bad parts of `express.js`.
## Example
```js
import micro from "micro";
import Router from "@synvox/router";
const app = Router();
app.get("/", (_req, _res) => "Hello World!");
micro(app).listen(3000);
```
`@synvox/router` is different than other `micro` routers because it enables nesting. This is very nice when you are combining routers together:
```js
import micro from "micro";
import Router from "@synvox/router";
import AuthService from "./auth";
import PeopleService from "./people";
const app = Router();
app.use("/auth", AuthService);
app.use("/people", PeopleService);
micro(app).listen(3000);
```
There is no `next` method and middleware is discouraged. _In `synvox/router`, the req object is a vanilla `http.IncomingMessage` object._ There is no `req.body`, `req.params`, `req.query`, etc. Instead try this:
```js
import micro from "micro";
import Router, { params, query } from "@synvox/router";
const app = Router();
app.get("/:name", req => {
const { name } = params(req);
const { sort } = query(req);
// do something with `name` and `sort`
return { ok: true };
});
micro(app).listen(3000);
```
You may also write your own hooks using `createHook`:
```js
import micro from "micro";
import Router, { params, body, createHook } from "@synvox/router";
const useUser = createHook(async req => {
const token = req.headers.token;
const user = token ? await Users.get(token) : null;
return user;
});
const app = Router();
app.get("/", async req => {
const user = await useUser(req);
// do something with `user`
return { ok: true };
});
micro(app).listen(3000);
```