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

mini-connect

v1.0.5

Published

High performance middleware framework like connect

Downloads

3

Readme

mini-connect

mini-connect is an extensible framework for node using "plugins" known as middleware like connect.

var connect = require('mini-connect');

var app = connect();

app.use(function(ctx, req, res, next){
  console.log('md1')
});

app.use(function(ctx, req, res) {
  console.log('md2')
})

function main() {
  let context = {};
  let req = {};
  let res = {};

  app(context, req, res);
}

main();

Getting Started

Connect is a simple framework to glue together various "middleware" to handle something like request, tcp, rpc.

Install Connect

$ npm install mini-connect

Create an app

The main component is a Connect "app". This will store all the middleware added and is, itself, a function.

var app = connect();

Use middleware

The core of Connect is "using" middleware. Middleware are added as a "stack" where incoming requests will execute each middleware one-by-one until a middleware does not call next() within it.

app.use(function middleware1(ctx, req, res, next) {
  // middleware 1
  next();
});
app.use(function middleware2(ctx, req, res, next) {
  // middleware 2
  next();
});

Mount middleware

The .use() method also takes an optional string that is matched against the beginning of the data.

app.use('afunc', function fooMiddleware(ctx, req, res, next) {
  // req.url starts with "/foo"
  next();
});
app.use('bfunc', function barMiddleware(ctx, req, res, next) {
  // req.url starts with "/bar"
  next();
});

Error middleware

There are special cases of "error-handling" middleware. There are middleware where the function takes exactly 4 arguments. When a middleware passes an error to next, the app will proceed to look for the error middleware that was declared after that middleware and invoke it, skipping any error middleware above that middleware and any non-error middleware below.

// regular middleware
app.use(function (ctx, req, res, next) {
  // i had an error
  next(new Error('boom!'));
});

// error middleware for errors that occurred in middleware
// declared before this
app.use(function onerror(err,ctx, req, res, next) {
  // an error occurred!
});

API

The Mini-connect API is very minimalist, enough to create an app and add a chain of middleware.

When the mini-connect module is required, a function is returned that will construct a new app when called.

// require module
var connect = require('mini-connect')

// create app
var app = connect([opts])

connect([opts])

The opts object has three functions: finalHandler(ctx, req, res), dispatch(dispatchCtx, route, req) and dispatchContext(). The finalHandle function must be return a function function(err){...}. The returned function is the last function in mini-connect middleware. It can handle error. The dispatch function return a Boolean value. if it return true, call this middleware 's handle or fn, else skip. The dispatchContext function return a object what pass to dispatch function as first parameter. You can give some status in it. you can find whole declare in test\server.js. These are some code:

var app = connect({
  finalHandler: (ctx, req, res) => {
    return function(err) {
      // do something
    }
  },
  dispatch: (ctx, route, req) => {
    return true;
  },
  dispatchContext: function() {
    return {};
  }
})

app(ctx, req, res[, next])

The app itself is a function. This is just an alias to app.handle.

app.handle(ctx, req, res[, out])

Calling the function will run the middleware stack against the given Node.js http request (req) and response (res) objects. An optional function out can be provided that will be called if the request (or error) was not handled by the middleware stack.

app.use(fn)

Use a function on the app, where the function represents a middleware. The function will be invoked for every request in the order that app.use is called. The function is called with three arguments:

app.use(function (ctx, req, res, next) {
})

In addition to a plan function, the fn argument can also be a Node.js HTTP server instance or another Connect app instance.

app.use(route, fn)

Use a function on the app, where the function represents a middleware. The function will be invoked for every request in which the string starts with the given route string in the order that app.use is called. The function is called with three arguments:

app.use('afunc', function (ctx, req, res, next) {
})

License

MIT

Thanks

Thanks for connect.