What is ware?
The 'ware' npm package is a middleware engine for handling a series of functions in a pipeline. It allows you to compose middleware functions and execute them in sequence, making it useful for tasks such as request handling, data processing, and more.
What are ware's main functionalities?
Middleware Composition
This feature allows you to compose multiple middleware functions and execute them in sequence. Each middleware function can modify the request and response objects and pass control to the next function in the pipeline.
const ware = require('ware');
const middleware = ware()
.use((req, res, next) => {
req.processed = true;
next();
})
.use((req, res, next) => {
res.message = 'Hello, world!';
next();
});
const req = {};
const res = {};
middleware.run(req, res, (err) => {
if (err) throw err;
console.log(req.processed); // true
console.log(res.message); // 'Hello, world!'
});
Error Handling
This feature demonstrates how 'ware' handles errors in the middleware pipeline. If an error is passed to the 'next' function, the subsequent middleware functions are skipped, and the error is handled in the final callback.
const ware = require('ware');
const middleware = ware()
.use((req, res, next) => {
next(new Error('Something went wrong'));
})
.use((req, res, next) => {
res.message = 'This will not be executed';
next();
});
const req = {};
const res = {};
middleware.run(req, res, (err) => {
if (err) {
console.error(err.message); // 'Something went wrong'
}
});
Other packages similar to ware
express
Express is a web application framework for Node.js, often used for building web servers and APIs. It has a robust middleware system similar to 'ware', allowing you to compose and execute middleware functions in sequence. However, Express is more feature-rich and specifically designed for web applications.
koa
Koa is a web framework designed by the creators of Express. It uses async functions and promises to handle middleware, providing a more modern and streamlined approach compared to 'ware'. Koa is also specifically designed for web applications and APIs.
compose-middleware
Compose-middleware is a lightweight middleware composition library similar to 'ware'. It allows you to compose and execute middleware functions in sequence. While it offers similar functionality, it is more focused on simplicity and minimalism.
ware
Easily create your own middleware layer.
Example
var ware = require('ware');
var middleware = ware()
.use(function (req, res, next) {
res.x = 'hello';
next();
})
.use(function (req, res, next) {
res.y = 'world';
next();
});
middleware.run({}, {}, function (err, req, res) {
res.x;
res.y;
});
Give it any number of arguments:
var ware = require('ware');
var middleware = ware()
.use(function (a, b, c, next) {
console.log(a, b, c);
next();
})
middleware.run(1, 2, 3);
Handles errors for you, just use a handler with an arity of +1
:
var ware = require('ware');
var middleware = ware()
.use(function (obj, next) {
if ('42' != obj.value) return next(new Error());
next();
})
.use(function (obj, next) {
console.log('yes!');
})
.use(function (err, obj, next) {
console.log('no!');
});
middleware.run({ life: '41' });
middleware.run({ life: '42' });
API
ware()
Create a new list of middleware.
.use(fn)
Push a middleware fn
onto the list. If the middleware has an arity of one more than the input to run
it's an error middleware.
.run(input..., [callback])
Runs the middleware functions with input...
and optionally calls callback(err, input...)
.
License
(The MIT License)
Copyright (c) 2013 Segment.io <friends@segment.io>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.