Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express JS equivalent of decorator pattern from Python frameworks

Working with Express js to write a simple NodeJS webservice. I'm historically a python guy.

In frameworks like Django or Flask, its common to see Python decorators used to implement logic from plugins only on specific endpoints. An example of this pattern can be seen here.

http://pythonhosted.org/Flask-Classy/#using-multiple-routes-for-a-single-view

I'm working on an Express middleware and have everything working well with the app.use 3-parity function, but this is only relevant for logic executes for every request. I'd like to allow the end user of the plugin to run parcels of my logic (already in separate functions) only on specific endpoints similar to the pattern outlined in the source above.

Some of the configuration to these wrappers would be passed at app start.

What would be the best approach to this? Should I emulate this pattern with functions that take the actual route handler as an argument and return it at end? Something like this?

function pluginWrapper(endptFunc){
    //plugin logic here
    return endptFunc;
  }

  app.get('/endpt', pluginWrapper(function(req,res,next){
    //endpt logic here
    res.end()
  }));
like image 651
DeaconDesperado Avatar asked Nov 06 '13 15:11

DeaconDesperado


People also ask

Are there decorators in JavaScript?

Decorators In JavaScript. In JavaScript, decorators are a stage 2 proposal but they can be used via Babel and the TypeScript compiler. JavaScript decorators are higher-order functions that receive a function argument, a class, or a member of a class and add to the functionality of that argument without modifying it.

Is NestJS better than Express?

As you already know, NestJS is written in and supports TypeScript. As TypeScript follows status typing and includes a “type” feature, Nest is more reliable and suitable to develop large-scale applications. Express doesn't support TypeScript because of which your application may not run on multiple browsers.

Is Nest based on Express?

Nest makes use the Express HTTP framework by default. However, Nest is platform agnostic, meaning it can work with any Node HTTP framework. For example, it can optionally be configured to use Fastify, which can improve the Node framework.

Is NestJS easier than Express?

Since ExpressJS doesn't follow MVC, there's no proper structure which makes the application inefficient and less optimized. NestJS becomes a better choice for developers as it is clearly based on an architecture with components like modules, controllers, and providers.


1 Answers

Here are the express idiomatic strategies:

  1. Things relavent for the majority of requests across the entire site become normal connect middleware: app.use(express.cookieParser())
  2. Things relavent for just a particular route can go just on that route: app.post('/users', express.bodyParser(), createUser). This is the pattern I think that most closely matches your above scenario
  3. Groups of related middleware can be passed as lists: app.get('/books', [paginate, queryLimit, memoize], getBooks). And of course that list could be a variable or module and thus shared in a DRY fashion.
  4. Common functionality triggered by patterns in the path itself can use app.param: app.get('/:username/hobbies', getHobbies)
  5. Existing regular functions can be wrapped into middleware to adapt them to a middleware API.
  6. You can just call functions as normal. Not every method of code reuse has to be shoehorned into one of express's convenient patterns.

To address your question more directly, I don't think you should try to port the python decorator pattern 1-to-1 to javascript. Middleware accomplishes essentially the same thing. If you post concrete examples using decorators, we can suggest an idiomatic way to implement them in express.

like image 78
Peter Lyons Avatar answered Oct 11 '22 17:10

Peter Lyons