1.0.2 • Published 4 years ago
expouter v1.0.2
Getting Started with Awesome Router
import "@vio/router";
import express, { Router } from "express";
const router = Router();
router.get("/", "UsersController.index");
const app = express();
app.use(router);
app.listen(5000);
Features
- Automatically bind controllers with their routes
- Group routes
- Scaffold resourceful routes
Milestone 0.1
- 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
- Fix types
- Add tests
- Add JSDocs
Milestone 0.2
Group notes
Milestone 0.3
Scaffold resourceful routes
Milestone 0.4
Support for other routers
API
Init
import { Router } from "express";
import { AwesomeRouter } from "Vio/AwesomeRouter";
const controllersPath = __dirname + "/Controllers";
const router = AwesomeRouter(Router(), controllersPath);
RouterGET | POST | PATCH | PUT | DELETE | HEAD
router.get("/", "UserController.index", [AuthMiddleware]);
Router.group
router.group(
"/users",
(route) => {
route.get("/", "UserController.index");
route.get("/:id", "UserController.show", [withUserMiddleware]);
},
[AuthMiddleware]
);
Router.resource
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
*/
router.resource("users", "UserController", {
except: ["destroy"],
});
/*
[GET] /users - UserController.index
[POST] /users - UserController.store
[GET] /users/:id - UserController.show
[PATCH/PUT] /users/:id - UserController.update
*/
router.resource("users", "UserController", {
only: ["destroy"],
});
/*
[DELETE] /users/:id - UserController.destroy
*/
Adding Middleware
router.resource("users", "UserController", {
middleware: {
all: [AuthController]
index: [WithUser]
}
});
Other
Override the default controllers directory
const controllersDir = __dirname + "/Controllers";
const router = AwesomeRouter({
controllersDir,
});