Giter VIP home page Giter VIP logo

ioreq's Introduction

IOReq

IOReq is a simple microservices module for Node.js which passes requests and data via Redis. This requires your project to use ioredis.

Usage

const Redis = require('ioredis');
const IOReq = require('ioreq');

async function hello() {
  let redis = new Redis({host: "127.0.0.1"});
  let ioreq = new IOReq(redis, {prefix: "test:"});
  // Second argument is optional. As of now, the only option is prefix.
  // In this case, all PUBLISH channels are prefixed with "test:".

  // Listen for a few requests:
  const listener = await ioreq.listen('my_endpoint');
  listener.on('request', (request, reply) => {
    if (request.please === 'say_hello') {
      reply({hello: 'world'});
    } else if (request.please === 'give_time') {
      let date = new Date();
      reply({
        hour: date.getHours(),
        minute: date.getMinutes(),
        year: date.getFullYear()
      });
    } else if (request.please === 'take_too_long') {
      setTimeout(() => reply({hello: "world, it's been a while"}), 5000)
    } else {
      reply({error: "No command provided!"});
    }
  });

  // Simple request:
  let result = await ioreq.request('my_endpoint', {please: 'say_hello'});
  console.log(`Hello, ${result.hello}!`) // Hello, world!

  // Request with lots of data:
  let res = await ioreq.request('my_endpoint', {please: 'give_time'});
  console.log(`Happy ${res.year}! The time is ${res.hour}:${res.minute}`);

  // A request which times out (optional third argument for milliseconds):
  try {
    let result = await ioreq.request('my_endpoint', {please: 'take_too_long'}, 3000);
  } catch (e) {
    console.log(e.message) // Request timed out.
  }
}

hello();

ioreq's People

Contributors

tphecca avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.