Using Decorators in Express

I wanted Decorators to be part of Express restful API system, where you can define the url, method etc using decorators. This exist in Java Spring Framework like

public class GreetingController {

    private static final String template = "Hello, %%s!";
    private final AtomicLong counter = new AtomicLong();

    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));


so in Express this

app.get('/greeting', function (req, res) {
  res.send('Hello World!');

could convert to

class GreetingController() {
    greeting(req, res) {
       res.send('Hello World!');

This way a lot of Java Developers would be interested in NodeJS and Express environment and MEAN 2 would be great because Angular 2 also uses decorators 😄

Author: Fantashit

1 thought on “Using Decorators in Express

  1. @mohammedzamakhan You could do it now, tho not with the syntax you’ve added here.

    var router = express.Router();
    function Request (method, path) {
      return function (fn) {
        router[method.toLowerCase()](path, fn);
    @Request('GET', '/greeting')
    function greeting (req, res) {}

    I’m not sure it’s really up to express to add that support, it’s more suited to a third party lib.

Comments are closed.