Router middleware for Groa gRPC framework.
Node.js v7.6+ is required, the middleware system of Gora is based on async function.
Install via NPM:
npm install groa-router --save
Create a router to manage your gRPC service and corresponding methods:
const Groa = require('groa');
const Router = require('groa-router');
const app = new Groa();
const router = new Router();
// Add proto file
app.addProto(__dirname + '/example.proto');
// package: example.foo
// service: Example1
// method: echo
router.rpc('/example.foo.Example1/Echo', async (ctx) => {
console.log('Echo');
ctx.body = ctx.req.body;
});
// Add router middleware
app.use(router.routes());
app.listen(50051, () => {
console.log('Listening on port 50051');
});
example.proto
syntax = "proto3";
package example.foo;
service Example1 {
rpc Ping(Echo) returns (Echo) {}
rpc Echo(Echo) returns (Echo) {}
rpc Hello(Hello) returns (Hello) {}
}
message Echo {
string content = 1;
}
message Hello {
string msg = 1;
}
Route paths can be prefixed at the router level:
// package: example.foo
// service: Example1
const router = new Router({
prefix: 'example.foo.Example1'
});
// method: echo
router.rpc('echo', async (ctx) => {
console.log('Echo');
ctx.body = ctx.req.body;
});
Licensed under the MIT License
Copyright(c) 2017 Fred Chien(錢逢祥) <cfsghost@gmail.com>