nodeway

2.0.5 • Public • Published

nodeway: 基于nodejs的微服务(Microservices)

一个微服务(Microservices)被定义为一个JS类(Class)。
nodeway直接发布这个JS类,使浏览器或nodejs可以远程调用。

Installing

# npm install nodeway -g

MacOS

# vi ~/.bash_profile
export NODE_PATH="/usr/local/lib/node_modules:/root/node_modules"

Linux

# vi ~/.bashrc
export NODE_PATH="/usr/local/lib/node_modules:/root/node_modules"

Using

# nodeway

  Usage: nodeway [options]

  Options:

    -h, --help                  output usage information
    -V, --version               output the version number
    --class <class>[,<class>]*  class list
    --host <host>               Listen ip or hostname, default '127.0.0.1'
    --port <port>               Listen port, default '80'
    --docs [root]               Httpd docs root, optional
    --config <file>             Config file

  Prompt:

    Either --class or --config must be one.

Example 'HelloWorld.js'

建立测试目录,在测试目录下创建'HelloWorld.js'微服务,内容如下:

const Nodeway = require("nodeway");
 
class HelloWorld extends Nodeway {
    constructor(uuid) {
        super(uuid);
    }
    async say(message) {
        this.broadcast('data', `${message}(${Nodeway.onlineCount})`);
        return new Promise(function(resolve, reject) {
            resolve(message);
        });
    }
}
 
module.exports = HelloWorld;

Run

# nodeway --config nodeway-helloworld/config.js &

Test

编写浏览器调用微服务测试文件'index.html',内容如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>HelloWorld</title>
    <script type="text/javascript" src="/HelloWorld.js"></script>
</head>
<body>
<script>
let api = new HelloWorld;
api.on('data', message => document.write(message+'<br/>'));
setInterval(async function(api) {
    try {
        let message = await api.say('Hello Safari!');
        document.write(message+'<br/>');
    }
    catch(e) {}
}, 6000, api);
</script>
</body>
</html>

在浏览器中访问'http://localhost:8080'进行测试。

编写nodejs调用微服务测试文件'test_from_nodejs.js',内容如下:

const requireFromUrl = require('require-from-url');
const readline = require('readline');
 
function main(HelloWorld) {
    let api = new HelloWorld;
    api.on('data', console.log);
 
    readline.createInterface({
        input: process.stdin,
        output: process.stdout
    })
    .on('line', async function(line) {
        try {
            let message = await api.say(line.trim());
            console.log(message);
        }
        catch(e) {}
    })
    .on('close', process.exit);
}
 
requireFromUrl(["http://localhost:8080/HelloWorld.js"], function(err, HelloWorld) {
    if(!err) main(HelloWorld);
});

执行node test_from_nodejs.js进行测试。

你也可以用命令npm install nodeway_helloworld直接安装这个测试例子。

License

MIT © May xiaoya zhang

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.0.5
    1
    • latest

Version History

Package Sidebar

Install

npm i nodeway

Weekly Downloads

88

Version

2.0.5

License

MIT

Unpacked Size

34.5 kB

Total Files

12

Last publish

Collaborators

  • may