Express Plumber

Well, programming languages are super awesome. Yes, there are lots of them and many alternatives to do the same task. But usually, there are only a few that stand up.

Most of the prominent programming languages are we used to use are imperative. For example Java, C++, and C#. In the imperative paradigm, we define step by step instructions to get what we what to. This approach is known to have issues such as immutable state, boilerplate code, and difficulty to work with concurrency.

Recently, the functional programming style is gaining traction and becoming a trend that has advantages such as not needing to worry about keeping the state, support for immutable variables, and higher-order functions. Though the functional approach has many amazing features, it’s also not good and fancy all the time. In functional programming, we describe what we need rather than how to do it. And when we do this using functions we declare them in the reverse order (first operation at last) which is not natural.

So there’s something that we have to sacrifice if we chose one of the sides. But what if we don’t. What about the best of both worlds. Is it possible? Well, I think yes. I guess you may have seen the observable pattern, the pattern used in ReactiveX libraries. There we create a pipe that has a source that produces data which gets transformed by sending it through a set of operators(functions) until we get the desired output, just like a pipeline. In this pattern, we define operators in imperative style. We don’t have to worry about the state between operators by ourselves, doesn’t have a limitation on immutability and since operators are just higher-order functions, we have the shiny stuff of functional programming style too.

So, a while ago I was working on a Node.js app and I thought it would be nice if it can be written as a pipeline. We just create a pipeline that converts a request to a response while doing all other operations such as DB access and logging. I tried some approaches and the bottleneck was defining the operator to do the transformations. But wait, we have RxJS library that has a complete set of operators right? I just plugged it in and wrote a wrapper for Express routes.

Hukx*

(*I’m bad at naming things)

Check out the source at GitHub. And also you can just plug it in using npm.

npm i hukx --save</span>

Once you have the package you can wrap any Express route to make it a pipeline that transforms requests to responses.

Initially, you have to import Express and initialize the router

const express = require('express')
const router = express.Router()</span>

Then you have to import Hukx and initialize it using the previously created express router.

const { hukx, piperOf } = require('hukx')
const piper = piperOf(router)</span>

We can use this piper object to define our routes. For example here is a simple endpoint that echoes “Hello World”.

piper.get('/', hukx.pipe(
  map(req => "Hello World")
))</span>

Here we are just mapping the requests into “Hello World” responses.

You may feel this as overkill, but it’s because this is just a simple echo endpoint. When you want to build more complex and practical endpoints you’ll get all the advantages of functional paradigm but with more readable and concise manner.

Finally, you just have to export the router to be used in the Express app.

module.exports = router</span>

A working sample is available in the sample directory of the repository.


This is a library I wrote in spare time while working on the actual project so it not optimal and can be improved much more. So any kind of contribution even a documentation or code sample is very welcomed.

For any issues, hugs or bugs,

My twitter handle is ThatUdeshUK and the email is mail@udesh.xyz

Also, thank you for reading this all along… :)

Comments (1)

Edidiong Asikpo's photo

I totally agree Udesh Kumarasinghe, the benefits of functional programming is amazing. It was hard to grasp it at first coming from imperative programming but it's been great so far.

Thanks for sharing.