
Company News
AWS Security Hub Adds Socket for Supply Chain Security
Socket is now in the AWS Security Hub Extended plan. Adopt it through AWS, apply committed spend, and block malicious open source packages.
connect2 is an extensible framework for node using "plugins" known as middleware like connect.
var connect = require('connect2');
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();
Connect is a simple framework to glue together various "middleware" to handle something like request, tcp, rpc.
$ npm install connect2
The main component is a Connect "app". This will store all the middleware added and is, itself, a function.
var app = connect();
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();
});
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();
});
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!
});
The connect2 API is very similar to connect.
When the connect2 module is required, a function is returned that will construct
a new app when called.
// require module
var connect = require('connect2')
// create app
var app = 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 connect2 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 {};
}
})
The app itself is a function. This is just an alias to app.handle.
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.
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.
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) {
})
Thanks for connect.
FAQs
High performance middleware framework like connect
The npm package connect2 receives a total of 2 weekly downloads. As such, connect2 popularity was classified as not popular.
We found that connect2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket is now in the AWS Security Hub Extended plan. Adopt it through AWS, apply committed spend, and block malicious open source packages.

Research
/Security News
Popular npm packages keyv and cacheable compromised.

Security News
A misconfiguration gave three Anthropic models internet access, and one, believing it was in a simulation, shipped a credential-stealing package to PyPI.