Define your Express routes in a nice, expressive way using TypeScript decorators!
- Basic usage
- API
- Decorators
ALL
(path:PathParams
)Controller
(root?:string
, options?:RouterOptions
)ControllerMiddleware
(first:RequestHandler
, ...middleware:RequestHandler[]
)DELETE
(path:PathParams
)GET
(path:PathParams
)HEAD
(path:PathParams
)Method
(httpMethod:string
, path:PathParams
)OPTIONS
(path:PathParams
)PATCH
(path:PathParams
)POST
(path:PathParams
)PUT
(path:PathParams
)Parent
(parentController:Function
)RouteMiddleware
(first:RequestHandler
, ...middleware:RequestHandler[]
)
- Classes
- Decorators
- Example app
- Common problems
- Good practices
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();
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
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
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
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
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
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
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
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
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
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
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
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
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
Public interface for the express-decorated-router library
Defined in ExpressDecoratedRouter.ts:42
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
Reset the library, freeing resources. You should call this method after calling applyRoutes()
Returns: ExpressDecoratedRouter
Defined in ExpressDecoratedRouter.ts:155
Thrown when an input of a @Parent decoration has not been decorated with @Controller
Extends: Error
Defined in errors/ParentControllerError.ts:4
The child controller
Defined in errors/ParentControllerError.ts:6
The parent controller
Defined in errors/ParentControllerError.ts:8
Thrown when a class decorated with @Parent was not annotated with @Controller
Extends: Error
Defined in errors/UnregisteredControllerError.ts:4
The controller
Defined in errors/UnregisteredControllerError.ts:6
An example app can be found in the example
directory.
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.
- Always call
ExpressDecoratedRouter.reset()
after applying your routes to free up resources