API Middleware (Using Express.js)

How to create and import middleware

Creating middleware

Adding Middleware files

All files in ./middleware/methods must be an exported object with all unique keys for any function in that directory not just the file because circular imports are used for one single import. Select individual keys that you need for your imports.

Importing Middleware

const { oneMiddlewareFunction, secondMiddlewareFunciton } = require('<PATH-TO-MIDDLEWARE-DIRECTORY>');// orconst allMiddlewareFunctions = require('<PATH-TO-MIDDLEWARE-DIRECTORY>');

Keep in mind that by default Node.js will import the index file if a file is not specified

Example Creating a Middleware file

All middleware function names must be unique so they are not overwritten

module.exports = {

emptyMiddlewareExample: (req, res, next) => { next(); } }