Socket
Book a DemoInstallSign in
Socket

express-router-decorators

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-router-decorators

Write your Express (4+) routers declaratively with ES2015 classes and ES decorators. TypeScript and Babel supported.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

express-router-decorators

Write your Express (4+) routers declaratively with ES2015 classes and ES decorators. TypeScript and Babel supported.

Example

Using express-router-decorators, you can define your Router controller as ES2015 class, which lets you keep cleaner and well-readable code. Remember to add @Root decorator to the class - this is a 'mounting point' of your router. Particular paths in your router are defined using @Path decorator, which takes the path param (either string or regexp, as supported by Express) as its first argument. Second argument expects an http method name (string), but it is optional and defaults to 'get' (see the list of all http methods supported by express).

import * as express from 'express';
import { bindControllers, Root, Path } from 'express-router-decorators';

const app = express();

@Root('/greet')
class Greeter {

  @Path('/hello')
  hello (req, res) {
    res.json({greeting: 'hello!'});
  }
}

bindControllers(app, Greeter);

app.listen(1221, () => console.log('server ready at localhost:1221'));

Inheritance is also supported. You can extend your router classes with other classes! The router created with Greeter class below will also have /hola route (inherited from SpanishGreeter class).

class SpanishGreeter {
  @Path('/hola')
  hola (req, res) {
    res.json({greeting: 'hola!'});
  }
}

@Root('/greet')
class Greeter extends SpanishGreeter {

  @Path('/hello')
  hello (req, res) {
    res.json({greeting: 'hello!'});
  }
}

Installation

npm install express-router-decorators --save

Configuration

TypeScript:

Make sure to add following two lines in the "compilerOptions" of your tsconfig.json file:

"experimentalDecorators": true,
"emitDecoratorMetadata": true

Babel:

You will need "transform-decorators-legacy" plugin (npm install babel-plugin-transform-decorators-legacy --save-dev) in your .babelrc file, eg:

{
  "presets": ["es2015", "stage-2"],
  "plugins": ["transform-decorators-legacy"]
}

API

@Root(rootRoute: string) - class decorator used to define the router's mounting point.

  • rootRoute: string - router's mounting point (as known from Express: app.use('/mountingpoint', router)).

@Path(pathRoute: string | RegExp, httpMethod?: string) - methods decorator that defines the routes within router's instance.

@Use - methods decorator that defines the router's specific middleware. Takes no arguments.

bindControllers(app: Express, ...clazz: Function[]) - the 'main' function that 'connects' the router(s) class(es) to the Express application's instance.

  • app: Express - Express application's instance (eg. const app = express()).
  • ...clazz: Function[] - (rest-parameters style) router class(es) decorated with @Root and @Path/@Use (see example).

License

MIT

FAQs

Package last updated on 26 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts