question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Decorators in Express

See original GitHub issue

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

@RestController
public class GreetingController {

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

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

ref: https://spring.io/guides/gs/rest-service/

so in Express this

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

could convert to

@RestController()
class GreetingController() {
    @Request('/greeting')
    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 😄

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Twippedcommented, Oct 17, 2016

@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.

1reaction
dougwilsoncommented, Oct 17, 2016

Ah, thanks guys 😃 So regardless of the merits of even handing decorators in Express, if it’s not even a part of core JavaScript, I don’t think now is the right time to support them.

FWIW, I did a quick Google and found https://www.npmjs.com/package/express-decorators which may do what you’re looking for @mohammedzamakhan

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express.js With Typescript Decorators in Examples - Medium
Let's take a look at how we can make our Express.js Server code cleaner with use of the Typescript Method Decorators.
Read more >
@decorators/express - npm
node decorators - decorators for express library. ... Start using @decorators/express in your project by running `npm i @decorators/express` ...
Read more >
How to Create Express Router Decorators with TypeScript
Class decorator: A controller decorator which handles base router information like path prefix, router-level middleware... Method decorator: A ...
Read more >
Routing with TypeScript decorators for node applications
A guide on how to use TypeScript decorators to make routing in MVC frameworks (like expressjs) easier and more enjoyable.
Read more >
OvernightJS: The best way to use ExpressJS with TypeScript
You may add routes to the controller by using decorators for Express GET, POST, PUT, and DELETE routes. Your method will work exactly...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found