Code Monkey home page Code Monkey logo

express-decorated-router's Introduction

Express Decorated Router

Define your Express routes in a nice, expressive way using TypeScript decorators!

NPM

Greenkeeper badge Coverage Status Build Status


Table of Contents

Basic usage

import * as express from 'express';
import {Controller, ControllerMiddleware, POST, RouteMiddleware, ExpressDecoratedRouter} from 'express-decorated-router';

@Controller('/auth')
@ControllerMiddleware(someMiddleware(), moreMiddleware())
class MyAuthController {

  @POST('/login')
  @RouteMiddleware(onlyApplyToThisRoute())
  public static login(req: express.Request, res: express.Response): void {
    doSomeMagic();
  }
}

const app: express.Application = express();

ExpressDecoratedRouter.applyRoutes(app);
ExpressDecoratedRouter.reset();

API

Decorators

ALL(path: PathParams)

Use this handler for any HTTP method

Returns: MethodDecorator

Parameters

Type Required Description
path PathParams ✔️ The path this handler will be responsible for

Defined in decorators/method/ALL.ts:8


Controller(root?: string, options?: RouterOptions)

Register this class as a controller

Returns: ClassDecorator

Parameters

Type Required Default value Description
root string "/" The root path for this controller
options RouterOptions Options passed to the Express router initialisation function.

Defined in decorators/Controller.ts:9


ControllerMiddleware(first: RequestHandler, ...middleware: RequestHandler[])

Define middleware for this controller. Any child controller which defines this class as its @Parent will inherit this middleware.

Returns: ClassDecorator

Parameters

Type Required Description
first RequestHandler ✔️ A middleware handler
middleware RequestHandler[] 0..n additional middleware handlers

Defined in decorators/ControllerMiddleware.ts:10


DELETE(path: PathParams)

Use this handler for the DELETE HTTP method

Returns: MethodDecorator

Parameters

Type Required Description
path PathParams ✔️ The path this handler will be responsible for

Defined in decorators/method/DELETE.ts:8


GET(path: PathParams)

Use this handler for the GET HTTP method

Returns: MethodDecorator

Parameters

Type Required Description
path PathParams ✔️ The path this handler will be responsible for

Defined in decorators/method/GET.ts:8


HEAD(path: PathParams)

Use this handler for the HEAD HTTP method

Returns: MethodDecorator

Parameters

Type Required Description
path PathParams ✔️ The path this handler will be responsible for

Defined in decorators/method/OPTIONS.ts:8


Method(httpMethod: string, path: PathParams)

Use this handler for the given HTTP method. The method must be one understood by Express' router.METHOD() method

Returns: MethodDecorator

Parameters

Type Required Description
httpMethod string ✔️ The HTTP method
path PathParams ✔️ The path this handler will be responsible for

Defined in decorators/method/Method.ts:10


OPTIONS(path: PathParams)

Use this handler for the OPTIONS HTTP method

Returns: MethodDecorator

Parameters

Type Required Description
path PathParams ✔️ The path this handler will be responsible for

Defined in decorators/method/HEAD.ts:8


PATCH(path: PathParams)

Use this handler for the PATCH HTTP method

Returns: MethodDecorator

Parameters

Type Required Description
path PathParams ✔️ The path this handler will be responsible for

Defined in decorators/method/PATCH.ts:8


POST(path: PathParams)

Use this handler for the POST HTTP method

Returns: MethodDecorator

Parameters

Type Required Description
path PathParams ✔️ The path this handler will be responsible for

Defined in decorators/method/POST.ts:8


PUT(path: PathParams)

Use this handler for the PUT HTTP method

Returns: MethodDecorator

Parameters

Type Required Description
path PathParams ✔️ The path this handler will be responsible for

Defined in decorators/method/PUT.ts:8


Parent(parentController: Function)

Define another controller as this controller's parent, inheriting its root path and middleware.

Returns: ClassDecorator

Parameters

Type Required Description
parentController Function ✔️ The parent controller

Defined in decorators/Parent.ts:7


RouteMiddleware(first: RequestHandler, ...middleware: RequestHandler[])

Define middleware for this route

Returns: MethodDecorator

Parameters

Type Required Description
first RequestHandler ✔️ A middleware handler
middleware RequestHandler[] 0..n additional middleware handlers

Defined in decorators/RouteMiddleware.ts:9


Classes

ExpressDecoratedRouter

Public interface for the express-decorated-router library

Defined in ExpressDecoratedRouter.ts:42

public static applyRoutes(app: IRouter)

Apply routes to the Express application. You should call reset() after calling this.

Returns: ExpressDecoratedRouter

Parameters

Type Required Description
app IRouter ✔️ The Express application
  • Throws: {ParentControllerError} If the input of a @Parent decoration has not been decorated with @Controller
  • Throws: {UnregisteredControllerError} If a class decorated with @Parent was not annotated with @Controller

Defined in ExpressDecoratedRouter.ts:139

public static reset()

Reset the library, freeing resources. You should call this method after calling applyRoutes()

Returns: ExpressDecoratedRouter

Defined in ExpressDecoratedRouter.ts:155


ParentControllerError

Thrown when an input of a @Parent decoration has not been decorated with @Controller

Extends: Error

Defined in errors/ParentControllerError.ts:4

public child

The child controller

Defined in errors/ParentControllerError.ts:6

public parent

The parent controller

Defined in errors/ParentControllerError.ts:8


UnregisteredControllerError

Thrown when a class decorated with @Parent was not annotated with @Controller

Extends: Error

Defined in errors/UnregisteredControllerError.ts:4

public controller

The controller

Defined in errors/UnregisteredControllerError.ts:6


Example app

An example app can be found in the example directory.

Common problems

Routes do not get registered

You must import/require the files containing your routes before you call applyRoutes(). When in doubt, set the DEBUG environment variable to express-decorated-router and see exactly what's going on.

Good practices

  • Always call ExpressDecoratedRouter.reset() after applying your routes to free up resources

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.