@kumori/http-message

HTTP server for Kumori platform

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@kumori/http-message
1.0.16 years ago6 years agoMinified + gzip package size for @kumori/http-message in KB

Readme

semantic-release
Http Message
Http server for Kumori Platform components.

Description

This library extends NodeJS HTTP server to support Kumori's channels (see Kumori's documentation for more information about channels and Kumori's service application model).
const http = require('@kumori/http-message')
let server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('okay');
})
server.listen(channel, (error) => {
    // Do something
})

This HTTP server is compatible with most of the existing http frameworks like ExpressJS.

Table of Contents

Hello World Server Hello World Client

Installation

Install it as a npm package
npm install -g @kumori/http-message

Usage

Kumori Platform incorporates a communication mechanism specifically designed for elastic software and is based on channels. A channel is an object a service component can use to communicate with other components. In this document we assume you already know what a Kumori component. If that is not the case, please refere to Kumori's documentation.
Http Message package has been developed specifically for Kumori's channels. It mimmics http NodeJS server API but changes some methods to use Kumori's channels instead of ports, IPs and/or domains. Let's see it in a couple of examples.

Simple Server

This is a simple hello world server encapsulated in a Kumori's component. It is based on the example used in NodeJS documentation.
const BaseComponent = require('component');
const http = require('@kumori/http-message');

class Component extends BaseComponent {

  constructor
    (runtime
    ,role
    ,iid
    ,incnum
    ,localData
    ,resources
    ,parameters
    ,dependencies
    ,offerings
    ) {
      super(runtime, role, iid, incnum, localData, resources, parameters, dependencies, offerings);
      this.httpChannel = offerings.endpoint;
  }

  run () {
    super.run();
    const server = http.createServer();
    server.on('request', (req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello World\n');
    });

    server.listen(this.httpChannel);
  }
}
module.exports = Component;

Simple Client

Http Message can be also used to communicate with an http server deployed as a Kumori component.
const BaseComponent = require('component');
const http = require('@kumori/http-message');

class Component extends BaseComponent {

  constructor
    (runtime
    ,role
    ,iid
    ,incnum
    ,localData
    ,resources
    ,parameters
    ,dependencies
    ,offerings
    ) {
      super(runtime, role, iid, incnum, localData, resources, parameters, dependencies, offerings);
      this.httpChannel = offerings.endpoint;
  }

  run () {
    super.run();
    const options = {
      channel: this.httpChannel,
      method: 'POST',
      path: '/upload'
    };
    const req = http.request(options, (res) => {
      res.on('data', (chunk) => {
        doThings(chunk)
      });
      res.on('end', () => {
        doThings()
      }
    });

    req.on('error', (e) => {
      doThings(e)
    });

    req.write(dataToSend);
    req.end();
  }
}
module.exports = Component;

WARNIG: currently, setTimeout and abort method are not supported.

License

MIT © Kumori Systems