sugo-endpoint-actor
Convert actor module methods into http endpoints
Last updated 7 months ago by realglobe .
Apache-2.0 · Repository · Bugs · Original npm · Tarball
$ cnpm install sugo-endpoint-actor 
SYNC missed versions from official npm registry.

sugo-endpoint-actor

Build Status npm Version JS Standard

Convert actor module methods into http endpoints

Requirements

Node.js NPM

Installation

$ npm install sugo-endpoint-actor --save

Usage

Create an instance and apply to sg-server (or sugo-cloud)

/** This is an example to use sugo-endpoint-actor */

'use strict'

const sgServer = require('sg-server')

const sugoActor = require('sugo-actor')
const { Module } = sugoActor()
const sugoEndpointActor = require('sugo-endpoint-actor')

// Define actor who receive methods from http
const httpActor = sugoActor({
  key: 'http',
  modules: {
    greeting: new Module({
      sayHello ({ msg }) {
        return `Hello! ${msg}`
      }
    })
  }
})

const server = sgServer({
  middlewares: [
    /* ... */
  ],
  endpoints: {
    // Exports actor module functions as http endpoint (like "/api/greeting/say-hello")
    '/api/:module/:method': { POST: sugoEndpointActor(httpActor) }
  }
})

server.listen(3000)


Then call the api from agent script.

/** This is example of client */

'use strict'

const arequest = require('arequest')
const co = require('co')

co(function * () {
  let request = arequest.create()
  let { body, statusCode } = yield request({
    method: 'POST',
    // Specify the module and method as url with hyphen-case name
    url: `http://localhost/api/greeting/say-hello`,
    json: true,
    body: {
      // Request body will be the first argument of the function
      msg: 'hoge'
    }
  })
  // Return values as response body
  console.log(body) // -> 'Hello! hoge'
}).catch((err) => console.error(err))

Signature

sugoEndpointActor(actor, options) -> function

Convert actor module methods into http endpoints

Args
Name Type Default Description
actor SugoActor Actor to exports
options object Optional settings.

License

This software is released under the Apache-2.0 License.

Links

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (12)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |