https://github.com/niklaus0823/matrixes-lib
Node.js module for create a gRPC server as Microservices or Koa server as ApiGateway.
https://github.com/niklaus0823/matrixes-lib
grpc grpc-gateway grpc-server node-module nodejs protoc typescript
Last synced: 9 months ago
JSON representation
Node.js module for create a gRPC server as Microservices or Koa server as ApiGateway.
- Host: GitHub
- URL: https://github.com/niklaus0823/matrixes-lib
- Owner: niklaus0823
- License: apache-2.0
- Created: 2018-02-27T03:22:56.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-08T02:29:34.000Z (almost 8 years ago)
- Last Synced: 2025-05-21T23:35:54.220Z (10 months ago)
- Topics: grpc, grpc-gateway, grpc-server, node-module, nodejs, protoc, typescript
- Language: JavaScript
- Homepage:
- Size: 43 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
matrixes-lib
=========================
Node.js module for create a gRPC server as Microservices or Koa server as ApiGateway.
## Aim
This project was forked from [agreatfool/sasdn](https://github.com/agreatfool/sasdn), and was intended to separate Command Tools and gRpc Server Source Code.
* other difference
* Remove bluebird, and use util.promisify
* The NPM package size is more littler, and install more faster
## Install
```bash
npm install matrixes-lib -save
```
## How to user
### Create gRPC Server
```typescript
import { RpcApplication } from 'matrixes-lib';
import { registerServices } from './services/Register';
class Server {
private _initialized: boolean;
public app: RpcApplication;
constructor() {
this._initialized = false;
}
public async init(isDev: boolean = false): Promise {
this.app = new RpcApplication();
this._initialized = true;
return Promise.resolve();
}
public start(): void {
if (!this._initialized) {
return;
}
registerServices(this.app);
const host = '0.0.0.0';
const port = '8080';
this.app.bind(`${host}:${port}`).start();
console.log(`server start, Address: ${host}:${port}!`);
}
}
const server = new Server();
server.init(process.env.NODE_ENV === 'development')
.then(() => {
server.start();
})
.catch((error) => {
console.log(`MicroService init failed error = ${error.stack}`);
});
process.on('uncaughtException', (error) => {
console.log(`process on uncaughtException error = ${error.stack}`);
});
process.on('unhandledRejection', (error) => {
console.log(`process on unhandledRejection error = ${error.stack}`);
});
```
### Create gRPC Client
```typescript
import * as grpc from 'grpc';
import {Duplex, Readable, Writable} from 'stream';
import {BookServiceClient} from './proto/book/book_grpc_pb';
import {Book, GetBookRequest, GetBookViaAuthorRequest} from './proto/book/book_pb';
import MSBookServiceClient from './clients/book/MSBookServiceClient';
const md = new grpc.Metadata();
md.set('name', 'fengjie');
let bookClient = new MSBookServiceClient('127.0.0.1:8080');
function getBook() {
const request = new GetBookRequest();
request.setIsbn(6);
bookClient.getBook(request, md)
.then((res) => {
console.log(`[getBook] response: ${JSON.stringify(res.toObject())}`);
console.log(`[getBook] done`);
})
.catch((err) => {
console.log(`[getBook] err: ${err.message}`);
console.log(`[getBook] done`);
});
}
getBook();
```
### Create API Gateway Server
```typescript
import {Koa, KoaBodyParser} from 'matrixes-lib';
import RouterLoader from './router/Router';
class Server {
private _initialized: boolean;
public app: Koa;
constructor() {
this._initialized = false;
}
public async init(isDev: boolean = false): Promise {
await RouterLoader.instance().init();
this.app = new Koa();
this.app.use(KoaBodyParser({ formLimit: '2048kb' }));
this.app.use(RouterLoader.instance().getRouter().routes());
this._initialized = true;
return Promise.resolve();
}
public start(): void {
if (!this._initialized) {
return;
}
const host = '0.0.0.0';
const port = '8081';
this.app.listen(port, host, () => {
console.log(`server start, Address: ${host}:${port}!`);
});
}
}
const server = new Server();
server.init(process.env.NODE_ENV === 'development')
.then(() => {
server.start();
})
.catch((error) => {
console.log(`Gateway init failed error = ${error.stack}`);
});
process.on('uncaughtException', (error) => {
console.log(`process on uncaughtException error = ${error.stack}`);
});
process.on('unhandledRejection', (error) => {
console.log(`process on unhandledRejection error = ${error.stack}`);
});
```
### Test API Gateway
```bash
# api
curl -d "isbn=1" "http://127.0.0.1:8081/v1/getBookUser"
# mock api when SET NODE_ENV=development
curl -d "isbn=1" "http://127.0.0.1:8081/v1/getBookUser?mock=1"
```
## Tool Chain
- [protoc-gen-grpc](https://github.com/niklaus0823/protoc-gen-grpc)
- [matrixes-cli](https://github.com/niklaus0823/matrixes-cli)
- [matrixes-lib](https://github.com/niklaus0823/matrixes-lib)
## Simple
- [matrixes-simple](https://github.com/niklaus0823/matrixes-simple)