-
-
Notifications
You must be signed in to change notification settings - Fork 588
/
Copy pathmethod-mw.js
54 lines (47 loc) · 1010 Bytes
/
method-mw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"use strict";
const ServiceBroker = require("../src/service-broker");
const util = require("util");
const MW = {
// Wrap local method calls
localMethod(handler, method) {
return params => {
console.log(
`The '${method.name}' method is called in '${method.service.fullName}' service.`,
params
);
const res = handler(params);
if (method.uppercase) return res.toUpperCase();
return res;
};
}
};
const broker = new ServiceBroker({
nodeID: "mw",
middlewares: [MW]
});
const svc = broker.createService({
name: "test",
actions: {
hello(ctx) {
return this.hello(ctx.params);
}
},
methods: {
hello: {
uppercase: true,
handler(params) {
return `Hello ${params.name}`;
}
},
hello2(params) {
return `Hello2 ${params.name}`;
}
}
});
broker
.start()
.then(() => broker.repl())
.then(() => {
broker.call("test.hello", { name: "John" }).then(res => broker.logger.info("Res:", res));
broker.logger.info("Svc", svc.hello({ name: "Jane" }));
});