express-http-decorators
v1.1.0
Published
Decorator typescript for express controlers/router
Downloads
1
Maintainers
Readme
express-http-decorators
Decorator typescript for express controlers/router
If you don't known about HTTP methods see HTTP request methods at MDN
How to use
// Controller example
import { Request, Response } from 'express'
import { ControllerBase, Controller, Get } from 'express-http-decorators';
@Controller() // /example
export class Example extends ControllerBase {
@Get({ path: '/world' }) // /example/world
world(request: Request, response: Response) {
response.send('Hello World');
}
@Post({ path: '/hello' }) // /example/hello
hello(request: Request, response: Response) {
response.send('Hello World');
}
}
export default Example;
// Express app example
import express, { Router } from 'express';
import { autoloader } from 'express-http-decorators';
import Example from './controllers/Example';
const app = express();
app.use(autoloader([Example], Router()))
app.listen(8888, () => {
console.log('Express http decorator on air at 127.0.0.1:8888')
})
Functions
Function to load and make endpoint
autoloader(array: Array<class extends ControlerBase>, router: Express.Router, verbose: (ResourceIdentifier) => void): Express.Router
: This function recives an array of class"controller"
that extendsControlerBase
and bind to router of expressarray: Array<class extends ControlerBase>
: Array of class that extendsControllerBase
router: Express.Router
: The router of expressverbose: (ResourceIdentifier) => void
: Function callback that is call every new endpoint binded to router (for debug)
HTTP methods decorators available: Post
, Get
, Put
, Patch
, Delete
@<decorator name>(methodParameters?: MethodParameters)
: With this decorator you can make a method at class as endpointMethodParameters.path
: Custom name to endpoint, if not setted, will be the name of class methodMethodParameters.middleware
: Array of middlewares express
Decorator for class controller. The class controller aways must be extends ControllerBase
.
@Controller(name?: string)
: This decorator defines the base name to endpoint see example above, if not setted, will be the name of class
For development or test
- Download the repository
npm install
To install dependecies
npm link && npm link express-http-decorators
To link this project as module in node_modules directory
npm run dev
To run code in example directory