Socket
Book a DemoInstallSign in
Socket

msg-router

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

msg-router

Simple intermediate layer router for data messages in JSON format

1.1.6
latest
Source
npmnpm
Version published
Weekly downloads
4
100%
Maintainers
1
Weekly downloads
 
Created
Source

msg-router

Simple intermediate layer router for data messages in JSON format

NPM version Dependency Status License Downloads

Installation

npm install msg-route --save

usage

This module can be used to route any data to their handlers. For more glkbokog understanding, consider the following example of routing telegram bot messages.

Eхample for telegram bot

stc/bot.main.js

const { Telegraf } = require('telegraf');
const MsgRouter = require('msg-router');

const bot = new Telegraf("your telegram token");
const router = new MsgRouter();

// connect routers
const routerForHelp = require('./routers/command.help.js')
router.use("/help", routerForHelp);

const routerForWeather = require('./routers/command.weather.js')
router.use("/weather", routerForWeather);



// the error handler must be the last in the handler chain. 
// The error handler function, unlike a regular handler, takes 3 arguments:
//  error   - an object with information about the error
//  message - the message, the processing of which led to the generation of 
//            an exception
//  next    - a function whose call will result in the transfer of control 
//            to the next handler (if when calling next (argument) the 
//            argument is not defined then the next handler will be called,
//            if defined, then the next error handler will be called
router.use((err, msg, next) => {
    // тут можно обработать ошибки
    console.log('bot.main.js', 0, msg.ctx.text);

    // или передать их на обработку дальше
    next(err);
});



// install a message handler that starts routing via MsgRouter
bot.on("message", (ctx) => {
    // form the path from the message text
    const targetPath = ctx.message.text.replace(/\s+/g,"/")

    // pass the path and object with the bot context to the router
    router.process({
        targetPath, 
        ctx
    });
})

// launch the bot
bot.launch()

the router files (src/routers/command.help.js and src/routers/command.weather.js) will be something like this:

const MsgRouter = require('msg-router');
const router = new MsgRouter();


// handler argument to / help command
router.use("/:args*", (msg, next) => {
    // this handler is called for all messages starting with "help" or 
    // "/help" command arguments will be in an array in msg.params.args
    // for a more complete understanding of the syntax used for path
    // see the module description:
    // https://www.npmjs.com/package/path-to-regex

    ...
    your handler code
    ... 

    // we can also reply to the user with msg.ctx.reply ("...")
    msg.ctx.reply("Српавку еще не завезли");
    
    // call next (); if we want to transfer processing to the next 
    // handlers. If next (argument); will be called with an argument,
    // this will ignore subsequent handlers and the next error handler
    // will be called. You can achieve the same behavior by calling an
    // error with throw
    next();
});


// Error handler
router.use((err, msg, next) => {
    // This handler will be called when an error occurs in the code of
    // the previous handlers of this router, as well as its sub-routers
    // if you send an error in their error handlers with the
    // next(error) command.
    ...
    your error handler code
    ... 

    next(err);
});

// exporting the router
module.exports = exports = router;

Another, simple, abstracted example

const MsgRouter = require('msg-router');


/*************************************** */
const route_aaa = new MsgRouter();
const route_bbb = new MsgRouter();
const route_ccc = new MsgRouter();


/*************************************** */
route_aaa.use((msg, next) => {
  console.log('route_aaa', 0, msg);
  next();
});


route_aaa.use('/aaa/bbb', route_bbb);

route_aaa.use('/aaa',(msg, next) => {
  console.log('route_aaa', 1, msg);
  next();
});

route_aaa.use('/aaa/:Aparam*',(msg, next) => {
  console.log('route_aaa', 1, msg);
  next();
});


route_aaa.use((err, msg, next) => {
  console.log('route_aaa', "ERROR", err, msg);
  next();
});


/*************************************** */
route_bbb.use((msg, next) => {
  console.log('route_bbb', 0, msg);
  // throw 456;
  next();
});

route_bbb.use('/ccc', route_ccc);

route_bbb.use(':Bparam*',(msg, next)=>{
  console.log("route_bbb", 1, msg);
  next();
});

route_bbb.use((err, msg, next) => {
  console.log('route_bbb', "ERROR", err, msg);
  next(err);
});



/*************************************** */
route_ccc.use((msg, next) => {
  console.log('route_ccc', 0, msg);
  next();
});

route_ccc.use('/:Cparam', (msg, next) => {
  console.log('route_ccc', 1, msg);
  next();
});

route_ccc.use((err, msg, next) => {
  console.log('route_ccc', "ERROR", err, msg);
  next(err);
});


/*************************************** */
// route_aaa.process({ targetPath: '/aaa', data: 'aaa' });
route_aaa.process({ targetPath: '/aaa/bbb/ccc', data: 'aaa' });

... documentation in processed

Participation in development

https://github.com/lastuniverse/msg-router/issues

License

MIT

NPM

Keywords

router

FAQs

Package last updated on 28 Oct 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.