Middleware pattern in fortjs

Ujjwal Gupta
fortjs
Published in
2 min readOct 24, 2019

Introduction

Middleware is a technique which is heavily used in nodejs frameworks like express etc. It is based on chain of responsibility pattern means one middleware will call another.

Middleware are nothing but a function which has access to request, response and next callback. They are called in the same order as defined.

e.g -

In express middleware is defined as

var express = require('express')
var app = express()
// myLogger is middleware
var myLogger = function (req, res, next) {
console.log('LOGGED')
next()
}

app.use(myLogger);

app.listen(3000)

The above code is taken from - https://expressjs.com/en/guide/writing-middleware.html

In the above code myLogger is a method and by using app.use we tell express that to use this middleWare.

Now what express does is - it will call the first middleware and provide the next method to the called middleware, so when the middleware has completed doing its task, it will call the next method & in turn next middleware in the chain is called.

Most of the expressjs plugins are created as middleware and available through npm.

So question is can we use those existing plugin in fortjs? Can we use middleware technique in fortjs ?

The answer is Yes, with very little customization we can use. Let’s see how ?

Let’s Code

FortJs provides different types of components which are in hierarchy.

  • Walls - Layer on top of App
  • Shield - Layer on top of Controller
  • Guard - Layer on top of Worker

As i said earlier, middleware are nothing but function, so we just need to call these method with required parameter - request, response & next.

The interesting thing is request, response is available to every components but what about next ?

We can use promise res value as next. Let’s see an example

var myLogger = function(req, res, next) {
console.log('LOGGED')
next()
}
return new Promise((res, rej) => {
myMiddleWare(this.request, this.response, res);
})

So now when myLogger will call the next , it will actually call the “res” which will resolve our promise.

Simple right ?

Let’s add the above code in a Wall component

import { Wall, textResult } from "fortjs";var myLogger = function(req, res, next) {
console.log('LOGGED')
next()
}
export class MyWall extends Wall {

async onIncoming() {
const result = await this.callMiddleWare(myLogger);
}
callMiddleWare(middleWare) {
return new Promise((res, rej) => {
myLogger(this.request, this.response, res);
});
}
}

In the above code -

  • We are calling our middleware in the incoming event.
  • The callMiddleWare method takes the middleware function and call it inside the promise and promise is resolved when middleware calls next.

How about we use something real , i mean a real & famous middleware .

Let’s use Helmet.js in a wall -

import { Wall, textResult } from "fortjs";
import * as helmet from 'helmet';
export class HelmetWall extends Wall {
async onIncoming() {
const result = await this.callMiddleWare(helmet());
}
callMiddleWare(middleWare) {
return new Promise((res, rej) => {
middleWare(this.request, this.response, res);
});
}
}

You see, we are able to use any middleware. And the advantage is that - you can use it in any component or controller .

The full code can be viewed at — https://github.com/ujjwalguptaofficial/fortjs-examples/tree/master/middleware

Thanks for reading it guys.

--

--

Ujjwal Gupta
fortjs
Editor for

Frontend lead Polygon | Creator of jsstore, mahaljs | sqlweb, fortjs | opensource creator | we3, blockchain, solidity | javascript