@kumori/http-message
HTTP server for Kumori platform
Last updated 2 months ago by jbgisbert .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @kumori/http-message 
SYNC missed versions from official npm registry.

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

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

Current Tags

  • 1.0.1                                ...           latest (2 months ago)

2 Versions

  • 1.0.1                                ...           2 months ago
  • 1.0.0                                ...           4 months ago
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 21
Dependencies (7)
Dev Dependencies (16)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |