Groa是Node.js的表达式gRPC中间件框架

网友投稿 1110 2022-10-25 15:35:06

Groa是Node.js的表达式gRPC中间件框架

Groa

Expressive gRPC middleware framework for Node.js. It provides the same style of middleware system and APIs many developers are familiar with which is similar to Koa 2.

Requirement

Node.js v7.6+ is required, the middleware system of Gora is based on async function.

Installation

Install via NPM:

npm install groa --save

Getting Started

The same way with Koa to implement your first gRPC server:

const Groa = require('groa');const app = new Groa();// Add proto fileapp.addProto(__dirname + '/example.proto');// Add middlewareapp.use(async (ctx, next) => { // response ctx.body = ctx.req.body;});app.listen(50051, () => { console.log('Listening on port 50051');});

example.proto

syntax = "proto3";package example.foo;service Example1 { rpc Ping(Ping) returns (Pong) {}}message Ping { string content = 1;}message Pong { string content = 1;}

Send Data as a Stream

Implement streaming method is quite easy that writing to Stream object of body directly.

const Groa = require('groa');const app = new Groa();// Add proto fileapp.addProto(__dirname + '/stream.proto');const delay = (interval) => { return new Promise((resolve) => { setTimeout(resolve, interval); });}// Add middlewareapp.use(async (ctx, next) => { // send a message ctx.body.write({ timestamp: new Date() }); // delay 1 second await delay(1000); // send a message ctx.body.write({ timestamp: new Date() }); // delay 1 second await delay(1000); // complete ctx.body.end();});app.listen(50051, () => { console.log('Listening on port 50051');});

stream.proto

syntax = "proto3";package example.foo;service Example1 { rpc receive(ReceiveRequest) returns (stream ReceiveResponse) {};}message ReceiveRequest {}message ReceiveResponse { string timestamp = 1;}

Receive Data from Stream

When input is a stream, ctx.req.body will be a stream object for receiving data. Besides, ctx.body contains the same stream object with ctx.req.body if output is stream as well.

const Groa = require('groa');const app = new Groa();// Add proto fileapp.addProto(__dirname + '/stream.proto');// Add middlewareapp.use(async (ctx, next) => { // Alias as ctx.body for input stream ctx.req.body.on('data', (data) => { console.log(data); // Send the same data back to client ctx.body.write(data); });});app.listen(50051, () => { console.log('Listening on port 50051');});

stream.proto

syntax = "proto3";package example.foo;service Example1 { rpc receive(stream ReceiveRequest) returns (stream ReceiveResponse) {};}message ReceiveRequest { string timestamp = 1;}message ReceiveResponse { string timestamp = 1;}

Status Code and Error Message

You can set status code and message when problem occurring for a request, the following is status code of gRPC Groa supported:

OK: 0CANCELLED: 1UNKNOWN: 2INVALID_ARGUMENT: 3DEADLINE_EXCEEDED: 4NOT_FOUND: 5ALREADY_EXISTS: 6PERMISSION_DENIED: 7RESOURCE_EXHAUSTED: 8FAILED_PRECONDITION: 9ABORTED: 10OUT_OF_RANGE: 11UNIMPLEMENTED: 12INTERNAL: 13UNAVAILABLE: 14DATA_LOSS: 15UNAUTHENTICATED: 16

Usage:

ctx.status = Groa.status.ABORTED;ctx.message = 'Something\'s wrong'

Or you can throw an error:

ctx.throw('wrong'); // UNKNOWN if no status codectx.throw(Application.status.OUT_OF_RANGE);ctx.throw(Application.status.OUT_OF_RANGE, 'OUT OF RANGE!!!');

Middlewares

Router: gora-router

Using Groa to build a Client with Promise-style functions

Groa provide a Client class which provides Promise-style functions to make client much easier in ES6.

const { Client } = require('groa');const client = new Client('0.0.0.0', 50051);const main = async () => { // Loading definition file await client.loadProto(__dirname + '/example.proto'); // Get service defnined let Example1 = client.getService('example.foo.Example1'); // call let ret = await Example1.ping({ content: 'hello' }); console.log(ret);};main();

TODO

Need more testcasesSupport google.api.http to generate restful API automaticallySupport SSL

License

Licensed under the MIT License

Authors

Copyright(c) 2017 Fred Chien(錢逢祥)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:#yyds干货盘点# leetcode算法题:反转链表
下一篇:SkipList跳表是怎么跳的?
相关文章