Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/niklashigi/decorate-express
- Owner: niklashigi
- Created: 2017-09-01T12:48:59.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-12-13T18:54:55.000Z (almost 5 years ago)
- Last Synced: 2024-11-04T01:52:08.561Z (16 days ago)
- Topics: decorators, express, express-middleware, typescript
- Language: TypeScript
- Homepage: https://npm.im/decorate-express
- Size: 74.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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/