npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

middleware-js

v2.0.0

Published

Application depending through middleware injection.

Downloads

16

Readme

Middleware.js

Travis npm version Codacy Badge Dependency Status devDependency Status Coverage Status License

Declare middleware dependencies from your modules, that are injectable to your modules. No more require('../../../../my/middleware')!

  // Instead the combersome path calculation. DONT DO THIS ANYMORE!
  var conf = require("../../../middleware/config");

  // Loads APP_DIR/middleware/config. DO THIS INSTEAD!
  var config = MiddlewareJs.require("middleware.config");

NPM

What is this?

  • Facility to easily require any module in your module or application without computing paths.
  • No more require('../../../../my/middleware/logging') in your code but just require('my.middleware.logging').
  • Your module or application requires something that only the user knows how to setup.
  • New Relic uses its own instance of Bunyan logger.
  • You want to provide freedom to users to choose whatever depedency they want.
  • Logging using Bunyan or Winston.

Installation

npm install --save middleware-js

MiddlewareJS Object

Requiring dependencies that range from a variety of modules needed by any Enterprise application:

  • Logging: Most applications use a logger. It is imperative for cloud applications to comply to standards.
  • Logger instances that write to the file-system breaks 12-factor apps.
  • 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.
  • 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.
  • Configuration: Building settings related to a given module is harder, specially because you don't know how your client uses configuration management.
  • 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.

Middleware.require(path)

The module computes the APP directory by default and loads the module at the given path.

  • path: Path to the middleware you want to be required. path-notation: You can use the path notation: "path/to/middleware". Specially when you are automating using files. dot-notation: You can use the dot notation: "path.to.middleware". Specially when you are automating using object-paths.

The file in the path can either a file or a directory:

  • file: Can be used as a very simple middleware such as constants, simple modules etc.
  • 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.
  // Loads APP_DIR/middleware/config.
  var config = MiddlewareJs.require("middleware.config");

The module loaded can be in either files:

  • APP_DIR/middleware/config.js: simpler configuration.
  • APP_DIR/middleware/config/index.js: more complex implementation with other sub-modules supporting it in the same directory.

See the test cases test/loadSettingsFromMiddleware.js for complete details.

Middleware.from(dirPath)

The module uses the given dirPath as the base dir and return the middleware itself as fluent API and, thus, must be used with load().

  • dirPath: the search path other than the current APP directory resolved by default.
  • Useful when loading anything outside your application directory.
  // Loads /shared/modules/default/logger
  var logger = MiddlewareJs.from("/shared/modules").require("default.logger");

  // Loads /secret-service/certs
  var secrets = MiddlewareJs.from("/secret-service").require("certs");

See the test cases test/loadSettingsFromMiddleware.js for complete details.

Error Handling

Since this is a synchronous library, attempting to load modules from computed paths that are non-existent will throw exceptions.

Also, remember that this module uses require() and the middleware loaded is cached as usual.

See the test cases test/loadSettingsWithErrors.js for complete details.

Use cases

  • New Relic: it uses their own logging capability. What if they required you to provide the logging capability instead?
  • Frameworks: You can be smart frameworks that requires middlewares in order to properly work.
  • This is a good example of designing a shared module using Interfaces.

Contributing

We use the GitFlow branching model http://nvie.com/posts/a-successful-git-branching-model/.

  1. Fork this project.
  2. Create your feature branch (git checkout -b feature/issue-444-Add-Rest-APIs origin/master --track)
  • Adding the Jira ticket ID helps communicating where this feature is coming from.
  1. Commit your changes (git commit -am 'Fix #444: Add support to REST-APIs')
  • Adding "fix #444" will trigger a link to the GitHub issue #444.
  1. Push to the branch (git push feature/issue-444-Add-Rest-APIS)
  2. Create new Pull Request as indicated by this page or your forked repo.