mikudos-node-app
v1.1.23
Published
gRPC microservice framework for fast implementation
Downloads
11
Readme
mikudos-node-app
mikudos-node-app is one Building block of Mikudos
build on top of Mali.js
The Application self is extended from Mali's Application
Example
A Example implementation of mikudos-node-app in typescript can be found as the linked repository.
Usage
Import the mikudos-node-app module:
import { Application } from 'mikudos-node-app';
const PROTO_PATH = path.resolve(
__dirname,
'../proto/game_puzzles/game_puzzles.proto'
);
const app: Application = new Application(PROTO_PATH);
// write a service to handle grpc methods
import {
Service,
Method,
HookMethod,
HookService,
App,
Customer,
} from 'mikudos-node-app';
/**
* You can find a systematic description for the use case of middleware at: https://mali.js.org/api/#mali-%E2%87%90-emitter
**/
async function hook1(ctx: any, next: Function) {
// TransactionManager.commitTransaction
console.log('GreeterService service hook');
await next();
}
async function hook2(ctx: any, next: Function) {
// TransactionManager.commitTransaction
console.log('SayHello method hook');
await next();
}
@Service({ name: 'GreeterService', serviceName: 'GreeterService' }) // register the service with name to add the service to app.services[name] at the same time
@HookService('before', hook1) // add one service level before hook
@HookService('after', hook1) // add one service level after hook
export class Greeter {
constructor(
@App() private app: Application,
@Customer('test string') private test: any
) {}
@Method('SayHello') // register method to a grpc method
@Method('SayHi') // register the same method to an other grpc method
@HookMethod('before', hook2) // register a method level middleware as before hook
@HookMethod('before', hook2) // register an other method level middleware as before hook
@HookMethod('after', hook2) // register a method level middleware as after hook
async SayHello(ctx: any, next: Function) {
// the handle method self
const app = ctx.app;
ctx.res = { message: 'Hello '.concat(ctx.req.name) };
next();
}
}
app.register(Greeter);
// for js implementation should use the version 1.0.16 of mikudos-node-app
var { Application } = require('mikudos-node-app');
const PROTO_PATH = path.resolve(
__dirname,
'../proto/game_puzzles/game_puzzles.proto'
);
const app: Application = new Application(PROTO_PATH);
Show Debug info
DEBUG=mikudos:* npm start