Browter gives the default Express Router some extra juice, at this moment it hasn't been tested in a real project nor does it have tests, so please wait till we've reached 0.2.0
- Grouping routes
- Binding route handlers thru controllers with a string format.
- Wraps your controllers to catch any async errors.
- Resourceful routes (W.I.P)
- Have all your routes in one file with no import boilerplate!
- Built on Typescript
install browter
yarn add @donnyroufs/browter
import "@donnyroufs/browter";
import express, { Router } from "express";
const app = express();
const apiRouter = Router();
apiRouter.group("users", (router) => {
router.get("/", "UserController.index");
});
apiRouter.group("posts", (router) => {
router.get("/", "PostController.index");
router.post("/", "PostController.store");
});
app.use("/api/v1", apiRouter);
app.listen(5000, () => console.log("Server running on: http://localhost:5000"));
- Automatically bind controllers with their routes
- Group routes
- Nested Grouping
- Scaffold resourceful routes
- Create an express router wrapper
- Allow to create routes which dynamically find the controller methods
- Set middleware for each route
- Set default Path for controllers
- Group Routes
- Fix types
- Add tests
- Add JSDocs
Nested Grouping of routes
Scaffold resourceful routes
Support for other routers Create an express factory that returns the modified version
Router[GET | POST | PATCH | PUT | DELETE | HEAD | ALL]
const router = express.Router();
router.group("/users", (router) => {
router.get("/", "UserController.index");
router.get("/:id", "UserController.show", [withUserMiddleware]);
});
router.resource("users", "UserController");
/*
[GET] /users - UserController.index
[POST] /users - UserController.store
[GET] /users/:id - UserController.show
[PATCH/PUT] /users/:id - UserController.update
[DELETE] /users/:id - UserController.destroy
*/
By default it loads the controllers from "Api/Controllers/index", if you prefer a different location then you can change it like so:
NOTE: It requires a singleton with a named export
import { setRouterConfig } from "@donnyroufs/browter";
setRouterConfig({
controllersDir: "App/Api/Controllers/Http/index",
});
Browter is built on top of this folder structure, however you can change it to anything you prefer.
> src
> Api
> Controllers
> index.ts
> Http
> User.controller.ts
> Bootstrap.ts
> Routes.ts
Without nested grouping
import { Router } from "express";
import {
ExampleMiddleware,
GlobalExampleMiddleware,
} from "./Middleware/ExampleMiddleware";
const router = Router();
router.group("/users", (router) => {
router.use(GlobalExampleMiddleware);
router.get("/", "UserController.index", [ExampleMiddleware]);
router.post("/", "UserController.store");
router.get("/:id", "UserController.show", [ExampleMiddleware]);
router.patch("/:id", "UserController.update");
router.delete("/:id", "UserController.destroy");
});
router.group("/posts", (router) => {
router.get("/", "PostController.index", [ExampleMiddleware]);
});
export default router;
Nested Grouping
router.group("/users", (router) => {
router.use(GlobalExampleMiddleware);
router.get("/", "UserController.index", [ExampleMiddleware]);
router.post("/", "UserController.store");
router.get("/:id", "UserController.show", [ExampleMiddleware]);
router.patch("/:id", "UserController.update");
router.delete("/:id", "UserController.destroy");
router.group("/posts", (router) => {
router.get("/", "PostController.index");
});
});