Node.js nut-pipe usage

nut-pipe npm package is an implementation of Chain of Responsibility Pattern. We can encapsulate or wrap every aspect of code in separate module or class or function, etc. So that, separated aspect modules would implement Single Responsibility Principle(SRP) and Seperation of Concern Principle(SoC) properly.

🙂 so if you want to implement OOP basics and principles, then you can use nut.pipe npm package.

This design provides to developer clean business logic code. They don't need to think about Error, Exception, Log handling business logic. Before their business logic code is called, pipeline is called and it means first middleware is triggered then first middleware will trigger next one, but no middleware knows about next middleware. Middleware should know about its business logic then should call next one.

I know too much words but developers want to see code 🙂 same code will be developed in different ways.

Code example v1

I wrote a greeting service with sayHello() and sayGoodbye() functions. They include all business logic in their body. This is not good approach definitely. Code duplications are everywhere 🙂 For example, every functions have try/catch, console.log('ENTRY: …'), console.log('SUCCESS: …'), console.log('ERROR: …'), etc. Notice that getFullName doesn't contain these business logics. So, we can't know whether it is called or not. If developer like me 🙂 doesn't write these kind of aspects(logging and exception handling is kind of aspect) then we never understand that that method is called in run-time.

Code example v2

nut.pipe npm package is developed for purpose of clean code. Notice that I have sayHello() and sayGoodbye() functions for business logic. Nut.pipe npm package handles error handling and log handling in separate functions. So that, sayHello() and sayGoodbye() functions have a very clean body. They include only business logic in their bodies. But, we have still exception and log handling(aspects).

Code Example v3

This is exactly same example in preceding code. But, middleware codes are saved in separate JavaScript files.

Leave a Reply