2.0.5 • Published 4 years ago

nodeway v2.0.5

Weekly downloads
3
License
MIT
Repository
-
Last release
4 years ago

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

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.3.8

7 years ago

1.3.7

7 years ago

1.3.6

7 years ago

1.3.5

7 years ago

1.3.4

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.9

7 years ago

1.2.8

7 years ago

1.2.7

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.5.11

8 years ago

0.5.10

8 years ago

0.5.9

8 years ago

0.5.8

8 years ago

0.5.7

8 years ago

0.5.6

8 years ago

0.5.5

8 years ago

0.5.4

8 years ago

0.5.3

8 years ago

0.5.2

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.9

8 years ago

0.4.8

8 years ago

0.4.7

8 years ago

0.4.6

8 years ago

0.4.5

8 years ago

0.4.4

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.10

8 years ago

0.3.9

8 years ago

0.3.8

8 years ago

0.3.7

8 years ago

0.3.6

8 years ago

0.3.5

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago