How to write a middleware class in Node.js
<p>I have researched the topic for hours on Google and books and I could only find very specific implementations. I'm struggling to write a simple Middleware class in node JS with only plain vanilla javascript (no additional module like async, co,..). My goal is to understand how it works not to get the most optimised code.</p>

<p>I would like something as simple as having a string and adding new string to it thru the use of middleware.</p>

<p>The class</p>

<pre><code>"use strict";
class Middleware {
    constructor() {
        this.middlewares = [];
    }
    use(fn) {
        this.middlewares.push(fn);
    }
    executeMiddleware(middlewares, msg, next) {
       // This is where I'm struggling
    }
    run(message) {
        this.executeMiddleware(this.middlewares, message, function(msg, next) {
            console.log('the initial message : '+ message);
        });
    }
}
module.exports = Middleware;
</code></pre>

<p>A possible usage</p>

<pre><code>const Middleware = require('./Middleware');
const middleware = new Middleware();

middleware.use(function(msg, next) {
    msg += ' World';
    next();
});

middleware.use(function(msg, next) {
    msg += ' !!!';
    console.log('final message : ' + msg);
    next();
});
middleware.run('Hello');
</code></pre>

<p>As a result the msg variable will end up being : 'Hello World !!!'</p>