Nanoscale Parts Manufacturing
Have ideas to improve npm?Join in the discussion! »

egg-grpc-servers

1.0.3 • Public • Published

egg-grpc-servers

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

npm install egg-plugin-grpc-server --save

Usage

// {app_root}/config/plugin.js
exports.grpcServer = {
  enable: true,
  package: 'egg-grpc-server',
};

Configuration

// {app_root}/config/config.default.js
exports.grpcServer = {
  port: 50051,
  timeOut: 5000,
  nameSpace: 'egg.node',
  dir: 'app/protos',
};

see config/config.default.js for more detail.

Example

  • proto文件夹中文件名({app_root}/app/protos/xxx.proto) proto文件中service名(service xxx) grpc文件夹中文件名({app_root}/app/grpc/xxx.js) 三者应当同名
  • proto/xxx.proto/service的接口名 grpc/xxx.js接口名 二者应当同名

proto 文件,可通过config.dir配置目录,默认app/proto

 
// {app_root}/app/protos/hello.proto
syntax = "proto3";
 
package egg.node;
 
service hello {
    rpc sayHello (helloRequest) returns (helloResponse) {};
}
 
message helloRequest {
    string name = 1;
    int32 group = 2;
}
 
message helloResponse {
    int32 code = 1;
    string message = 2;
}
 

接口实现,thiseggApplication,接受一个参数为请求的request

 
// {app_root}/app/grpc/hello.js
'use strict';
 
exports.sayHello = async function(request) {
  return {
    code: 200,
    message: 'hello ' + request.name + ', you are in ' + request.group,
  };
};
 

单元测试

 
const { assert, app } = require('egg-mock/bootstrap');
const path = require('path');
const PROTO_PATH_HELLO = path.join(__dirname, '../app/proto/hello.proto');
const grpc = require('grpc');
describe('test/grpc-server.test.js', () => {
  beforeEach(() => {
    return app.ready();
  });
  it('should visit hello by grpcServer.hello', async () => {
    const { test } = app.grpcServer;
    const res = await test.call(app, {
      proto: PROTO_PATH_HELLO,
      implement: 'sayHello',
      data: { name: 'leo', group: 1 },
    });
    assert(res.code === 200);
  });
  

Questions & Suggestions

Please open an issue here.

License

MIT

Install

npm i egg-grpc-servers

DownloadsWeekly Downloads

3

Version

1.0.3

License

MIT

Unpacked Size

11.5 kB

Total Files

10

Last publish

Collaborators

  • avatar