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()
}));
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.
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.
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.
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.
Here are the express idiomatic strategies:
app.use(express.cookieParser())
app.post('/users', express.bodyParser(), createUser)
. This is the pattern I think that most closely matches your above scenarioapp.get('/books', [paginate, queryLimit, memoize], getBooks)
. And of course that list could be a variable or module and thus shared in a DRY fashion.app.param
: app.get('/:username/hobbies', getHobbies)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With