fortjs
Published in

fortjs

Middleware pattern in fortjs

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.

--

--

--

Fort based MVC web framework for nodejs targeting good code structures & modularity.

Recommended from Medium

Is MEAN Stack The Future Of Web App Development?

Creating Favorites List Using LocalStorage in React [Part I]

Apply multiple functors as arguments to a function (Applicatives)

ES6 — The new Generation of JavaScript — Introduction

The 30 minute javascript guide

AlgoExpert vs. HackerRank vs. LeetCode

Snowpack: A Modern, Lightweight Build Tool for Faster Web Development

Snowpack’s logo and slogan.

Outfit Roundup

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ujjwal Gupta

Ujjwal Gupta

Creator of jsstore, sqlweb, fortjs

More from Medium

Angular, AWS Amplify manage mutiple environment diff based backend in easy way

How to Setup Express + Typescript + Eslint + Webpack 5

JS, Git & NoSQL

DDD with various programming languages in my way: part1 typescript