Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/niklashigi/decorate-express

:tulip: Route decorators for Express.
https://github.com/niklashigi/decorate-express

decorators express express-middleware typescript

Last synced: about 22 hours ago
JSON representation

:tulip: Route decorators for Express.

Awesome Lists containing this project

README

        

# decorate-express

[![Travis CI][travis-badge]][travis-link]
[![NPM][npm-badge]][npm-link]
[![Greenkeeper][greenkeeper-badge]][greenkeeper-link]

> This is a fork of [@stewartml](https://github.com/stewartml)'s no longer actively maintained [`express-decorators` package](https://github.com/stewartml/express-decorators).

Decorators for easily wiring up controller classes to [Express](http://expressjs.com/) routes.

## Installation

```
$ npm install --save decorate-express
```

## Example

```ts
import { BasePath, Get, Use, registerRoutes } from 'decorate-express'
import myMiddlewareFunction from './middleware'
import express from 'express'

@BasePath('/test')
public class TestController {
constructor(target) {
this.target = target
}

@Get('/hello', myMiddlewareFunction)
async sayHelloAction(request, response) {
response.send(`Hello, ${this.target}!`)
}

@Use()
async otherMiddleware(request, response, next) {
// This middleware will be called for every action.
next()
}
}

let app = express()
let test = new TestController('world')
registerRoutes(app, test)
```

When can now go to `/test/hello` and get `Hello, world!` back.

## Notes

* Actions are called with the correct context (i.e. `this` is an instance of the class).
* Actions can return [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)'s or be [`async` functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) and errors will get handled properly.

## API

### `@BasePath(path: string)`

Class decorator to add a base path to every route defined in the class.

### `@Middleware(fn: Middleware)`

If `fn` is a function, then the function is added as route-specific middleware for the action. Note that the middleware will be bound to the controller instance.

If `fn` is a string, then the method with that name will be exectued as route-specific middleware when the action is invoked.

### `@Param(param: string)`

Marks the method as a handler for all routes that use the specified parameter. This can be useful if you want to do something with it before it's passed on to the actual route handler, for example converting a string to an integer:

```js
@Param('id')
idParam(request, response, next, id) {
request.params.id = parseInt(request.params.id)
next()
}
```

### `@Route(method: string, path: string, middleware: Middleware[])`

Marks the method as a handler for the specified path and HTTP method.

#### HTTP method shortcuts

Instead of passing the HTTP method into the `@Route` decorator you can also use one of the provided shortcuts. `@Get('/path')` for example is equivalent to `@Route('get', '/path')`.

* `@All`
* `@Delete`
* `@Get`
* `@Options`
* `@Patch`
* `@Post`
* `@Put`
* `@Use`

### `getRoutes(target: Object): Route[]`

Gets the route metadata for the target object. Paths are automatically prefixed with a base path if one was defined.

### `registerRoutes(router: Express.Router, target: Object)`

Registers the routes found on the target object with an express Router instance.

## Questions, comments?

Please feel free to start an issue or offer a pull request.

[travis-badge]: https://img.shields.io/travis/shroudedcode/decorate-express.svg
[travis-link]: https://travis-ci.org/shroudedcode/decorate-express

[npm-badge]: https://img.shields.io/npm/v/decorate-express.svg
[npm-link]: https://www.npmjs.com/package/decorate-express

[greenkeeper-badge]: https://badges.greenkeeper.io/shroudedcode/decorate-express.svg
[greenkeeper-link]: https://greenkeeper.io/