A tiny routing library to complement micro inspired by the good and bad parts of express.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:
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:
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
:
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);