Code Monkey home page Code Monkey logo

browter's Introduction

example

Getting Started with Browter

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

Features

  • Automatically bind controllers with their routes
  • Group routes
  • Nested Grouping
  • Scaffold resourceful routes

Milestone 0.2

  • 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

Milestone 0.3

Nested Grouping of routes

Milestone 0.4

Scaffold resourceful routes

Milestone 0.5

Support for other routers Create an express factory that returns the modified version

API

Router.group

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 (milestone 0.4.0)

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
*/

Controllers Path

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",
});

Examples

Folder Structure

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

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.