{"id":14982243,"url":"https://github.com/marcellodesales/middleware-js","last_synced_at":"2026-03-10T13:05:25.370Z","repository":{"id":57296804,"uuid":"47049426","full_name":"marcellodesales/middleware-js","owner":"marcellodesales","description":"A Node.js middleware framework that allows dependency injection from modules.","archived":false,"fork":false,"pushed_at":"2015-12-27T22:36:26.000Z","size":19,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-17T03:37:00.774Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcellodesales.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-29T04:09:17.000Z","updated_at":"2016-01-16T14:39:40.000Z","dependencies_parsed_at":"2022-09-01T13:01:52.040Z","dependency_job_id":null,"html_url":"https://github.com/marcellodesales/middleware-js","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcellodesales%2Fmiddleware-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcellodesales%2Fmiddleware-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcellodesales%2Fmiddleware-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcellodesales%2Fmiddleware-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcellodesales","download_url":"https://codeload.github.com/marcellodesales/middleware-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242250923,"owners_count":20096896,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T14:05:00.884Z","updated_at":"2025-12-06T13:01:26.933Z","avatar_url":"https://github.com/marcellodesales.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Middleware.js\n\n[![Travis](https://api.travis-ci.org/marcellodesales/middleware-js.svg)](https://travis-ci.org/marcellodesales/middleware-js) [![npm version](https://badge.fury.io/js/middleware-js.svg)](http://badge.fury.io/js/middleware-js) [![Codacy Badge](https://www.codacy.com/project/badge/172621abbd81457d84ee5df6ebe13f91)](https://www.codacy.com/app/marcellodesales/middleware-js) [![Dependency Status](https://david-dm.org/marcellodesales/middleware-js.svg)](https://david-dm.org/marcellodesales/middleware-js) [![devDependency Status](https://david-dm.org/marcellodesales/middleware-js/dev-status.svg)](https://david-dm.org/marcellodesales/middleware-js#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/marcellodesales/middleware-js/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/marcellodesales/middleware-js?branch=master) ![License](https://img.shields.io/badge/license-MIT-lightgray.svg)\n\nDeclare middleware dependencies from your modules, that are injectable to your modules. No more `require('../../../../my/middleware')`!\n\n```js\n  // Instead the combersome path calculation. DONT DO THIS ANYMORE!\n  var conf = require(\"../../../middleware/config\");\n\n  // Loads APP_DIR/middleware/config. DO THIS INSTEAD!\n  var config = MiddlewareJs.require(\"middleware.config\");\n```\n\n[![NPM](https://nodei.co/npm/middleware-js.png?downloads=true\u0026downloadRank=true\u0026stars=true)](https://nodei.co/npm/middleware-js/)\n\n# What is this?\n\n* Facility to easily `require` any module in your module or application without computing paths.\n * No more `require('../../../../my/middleware/logging')` in your code but just `require('my.middleware.logging')`.\n* Your module or application requires something that only the user knows how to setup.\n * New Relic uses its own instance of Bunyan logger.\n* You want to provide freedom to users to choose whatever depedency they want.\n * Logging using Bunyan or Winston.\n\n# Installation\n\n```sh\nnpm install --save middleware-js\n```\n\n# MiddlewareJS Object\n\nRequiring dependencies that range from a variety of modules needed by any Enterprise application:\n\n* *Logging*: Most applications use a logger. It is imperative for cloud applications to comply to standards.\n * Logger instances that write to the file-system breaks 12-factor apps.\n * You cannot control the logger instance from the required module. For instance, the module `newrelic` creates its own logger which writes to the file system.\n * The instance you use is different than the module. For instance, some use `Bunyan` while others `Winston`. However, both uses the same interface methods for logging as `log.info`, `log.error`, etc.\n* *Configuration*: Building settings related to a given module is harder, specially because you don't know how your client uses configuration management.\n * You can require your clients to provide settings in a given directory. For instance, the module `newrelic` loads a file from the root directory. They could provide a different loader function that loads the properties from your configuration management such as `nconf`.\n\n## Middleware.require(path)\n\nThe module computes the APP directory by default and loads the module at the given path. \n\n* *path*: Path to the middleware you want to be required.\n *path-notation*: You can use the path notation: \"path/to/middleware\". Specially when you are automating using files.\n *dot-notation*: You can use the dot notation: \"path.to.middleware\". Specially when you are automating using object-paths.\n\nThe file in the path can either a file or a directory:\n\n* *file*: Can be used as a very simple middleware such as constants, simple modules etc.\n* *directory*: When dealing with complex middleware implementation, you may have multiple sub-modules that implement the given middleware. Very useful to only expose the middleware and not its implementation details.\n\n```js\n  // Loads APP_DIR/middleware/config.\n  var config = MiddlewareJs.require(\"middleware.config\");\n```\n\nThe module loaded can be in either files:\n\n* APP_DIR/middleware/config.js: simpler configuration.\n* APP_DIR/middleware/config/index.js: more complex implementation with other sub-modules supporting it in the same directory.\n\nSee the test cases `test/loadSettingsFromMiddleware.js` for complete details.\n\n## Middleware.from(dirPath)\n\nThe module uses the given dirPath as the base dir and return the middleware itself as fluent API and, thus, must be used with `load()`.\n\n* *dirPath*: the search path other than the current APP directory resolved by default.\n * Useful when loading anything outside your application directory.\n\n```js\n  // Loads /shared/modules/default/logger\n  var logger = MiddlewareJs.from(\"/shared/modules\").require(\"default.logger\");\n\n  // Loads /secret-service/certs\n  var secrets = MiddlewareJs.from(\"/secret-service\").require(\"certs\");\n```\n\nSee the test cases `test/loadSettingsFromMiddleware.js` for complete details.\n\n# Error Handling\n\nSince this is a synchronous library, attempting to load modules from computed paths that are non-existent will throw exceptions.\n\nAlso, remember that this module uses `require()` and the middleware loaded is cached as usual.\n\nSee the test cases `test/loadSettingsWithErrors.js` for complete details.\n\n# Use cases\n\n* *New Relic*: it uses their own logging capability. What if they required you to provide the logging capability instead?\n* *Frameworks*: You can be smart frameworks that requires middlewares in order to properly work.\n * This is a good example of designing a shared module using Interfaces.\n\n# Contributing\n\nWe use the GitFlow branching model http://nvie.com/posts/a-successful-git-branching-model/.\n\n1. Fork this project.\n2. Create your feature branch (`git checkout -b feature/issue-444-Add-Rest-APIs origin/master --track`)\n * Adding the Jira ticket ID helps communicating where this feature is coming from.\n3. Commit your changes (`git commit -am 'Fix #444: Add support to REST-APIs'`)\n * Adding \"fix #444\" will trigger a link to the GitHub issue #444.\n4. Push to the branch (`git push feature/issue-444-Add-Rest-APIS`)\n5. Create new Pull Request as indicated by this page or your forked repo.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcellodesales%2Fmiddleware-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcellodesales%2Fmiddleware-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcellodesales%2Fmiddleware-js/lists"}