libflitter/middleware/Middleware.js

/**
 * @module libflitter/middleware/Middleware
 */

const { Injectable } = require('flitter-di')

/**
 * The base class for all app-space middleware.
 * @extends module:flitter-di/src/Injectable~Injectable
 */
class Middleware extends Injectable {
    /**
     * Defines the services required by this unit.
     * The 'app' service is included by default.
     * @returns {Array<string>}
     */
    static get services() {
        return [...super.services, 'app', 'output']
    }

    /**
     * Executes the middleware. This method is called when a request
     * flows through a route that includes this middleware. Calling the
     * next() method allows the request flow to continue.
     * @param {express/request} req - the Express request
     * @param {express/response} res - the Express response
     * @param {Function} next - handler that, if called, allows the request flow to continue
     * @param args - optional arguments passed in from the call to {module:libflitter/middleware/MiddlewareUnit~MiddlewareUnit#mw}
     */
    async test(req, res, next, args = {}){
        this.output.warn(`Default middleware handler called. This method should be overridden by child classes. When called, it allows the request to proceed every time.`)
        next()
    }
    
}

module.exports = exports = Middleware