Rule

  • rule is reusable script, that can be used in multiple middlewares

  • RuleContext ctx is passed into rule function as first parameter

  • each middleware has own options script

More examples in middleware section

example.options.js
// This is defined in app middleware
module.exports = async function options(ctx) {
// return app specific options for rule
return {
host: 'localhost',
port: 3000
}
}
example.rule.js
const net = require('net');
const { pipeline } = require('stream');
module.exports = async function rule(ctx) {
// ctx.options is function from example.options.js above
const options = await ctx.options(ctx);
/*
{
host: 'localhost',
port: 3000
}
*/
ctx.on('connection', socket => {
const forwardSocket = net.connect(options)
// pipe streams
pipeline(socket, forwardSocket, () => {})
pipeline(forwardSocket, socket, () => {})
});
};