Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
/ router Public archive

A tiny routing library to complement micro inspired by the good and bad parts of express.js.

License

Notifications You must be signed in to change notification settings

Synvox/router

Repository files navigation

@synvox/router

travis ci codecov

A tiny routing library to complement micro inspired by the good and bad parts of express.js.

Example

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);

About

A tiny routing library to complement micro inspired by the good and bad parts of express.js.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published