{"id":28977603,"url":"https://github.com/niklaus0823/matrixes-lib","last_synced_at":"2025-06-24T15:04:06.231Z","repository":{"id":57292567,"uuid":"123066735","full_name":"niklaus0823/matrixes-lib","owner":"niklaus0823","description":"Node.js module for create a gRPC server as Microservices or Koa server as ApiGateway.","archived":false,"fork":false,"pushed_at":"2018-06-08T02:29:34.000Z","size":44,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-21T23:35:54.220Z","etag":null,"topics":["grpc","grpc-gateway","grpc-server","node-module","nodejs","protoc","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/niklaus0823.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-27T03:22:56.000Z","updated_at":"2022-11-24T02:33:46.000Z","dependencies_parsed_at":"2022-08-27T12:21:00.393Z","dependency_job_id":null,"html_url":"https://github.com/niklaus0823/matrixes-lib","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/niklaus0823/matrixes-lib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niklaus0823%2Fmatrixes-lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niklaus0823%2Fmatrixes-lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niklaus0823%2Fmatrixes-lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niklaus0823%2Fmatrixes-lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/niklaus0823","download_url":"https://codeload.github.com/niklaus0823/matrixes-lib/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niklaus0823%2Fmatrixes-lib/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261508112,"owners_count":23169627,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grpc","grpc-gateway","grpc-server","node-module","nodejs","protoc","typescript"],"created_at":"2025-06-24T15:01:22.422Z","updated_at":"2025-06-24T15:04:06.224Z","avatar_url":"https://github.com/niklaus0823.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"matrixes-lib\n=========================\nNode.js module for create a gRPC server as Microservices or Koa server as ApiGateway.\n\n## Aim\n\nThis project was forked from [agreatfool/sasdn](https://github.com/agreatfool/sasdn), and was intended to separate Command Tools and gRpc Server Source Code.\n\n* other difference\n  * Remove bluebird, and use util.promisify\n  * The NPM package size is more littler, and install more faster\n\n## Install\n\n```bash\nnpm install matrixes-lib -save\n```\n\n## How to user\n\n### Create gRPC Server\n\n```typescript\nimport { RpcApplication } from 'matrixes-lib';\nimport { registerServices } from './services/Register';\n\nclass Server {\n    private _initialized: boolean;\n    public app: RpcApplication;\n\n    constructor() {\n        this._initialized = false;\n    }\n\n    public async init(isDev: boolean = false): Promise\u003cany\u003e {\n        this.app = new RpcApplication();\n        this._initialized = true;\n\n        return Promise.resolve();\n    }\n\n    public start(): void {\n        if (!this._initialized) {\n            return;\n        }\n\n        registerServices(this.app);\n\n        const host = '0.0.0.0';\n        const port = '8080';\n        this.app.bind(`${host}:${port}`).start();\n        console.log(`server start, Address: ${host}:${port}!`);\n    }\n}\n\n\nconst server = new Server();\nserver.init(process.env.NODE_ENV === 'development')\n    .then(() =\u003e {\n        server.start();\n    })\n    .catch((error) =\u003e {\n        console.log(`MicroService init failed error = ${error.stack}`);\n    });\n\nprocess.on('uncaughtException', (error) =\u003e {\n    console.log(`process on uncaughtException error = ${error.stack}`);\n});\n\nprocess.on('unhandledRejection', (error) =\u003e {\n    console.log(`process on unhandledRejection error = ${error.stack}`);\n});\n```\n\n### Create gRPC Client\n\n```typescript\nimport * as grpc from 'grpc';\nimport {Duplex, Readable, Writable} from 'stream';\nimport {BookServiceClient} from './proto/book/book_grpc_pb';\nimport {Book, GetBookRequest, GetBookViaAuthorRequest} from './proto/book/book_pb';\nimport MSBookServiceClient from './clients/book/MSBookServiceClient';\n\nconst md = new grpc.Metadata();\nmd.set('name', 'fengjie');\n\nlet bookClient = new MSBookServiceClient('127.0.0.1:8080');\n\nfunction getBook() {\n    const request = new GetBookRequest();\n    request.setIsbn(6);\n\n    bookClient.getBook(request, md)\n        .then((res) =\u003e {\n            console.log(`[getBook] response: ${JSON.stringify(res.toObject())}`);\n            console.log(`[getBook] done`);\n        })\n        .catch((err) =\u003e {\n            console.log(`[getBook] err: ${err.message}`);\n            console.log(`[getBook] done`);\n        });\n}\n\ngetBook();\n```\n\n### Create API Gateway Server\n\n```typescript\nimport {Koa, KoaBodyParser} from 'matrixes-lib';\nimport RouterLoader from './router/Router';\n\nclass Server {\n    private _initialized: boolean;\n    public app: Koa;\n\n    constructor() {\n        this._initialized = false;\n    }\n\n    public async init(isDev: boolean = false): Promise\u003cany\u003e {\n\n        await RouterLoader.instance().init();\n\n        this.app = new Koa();\n        this.app.use(KoaBodyParser({ formLimit: '2048kb' }));\n        this.app.use(RouterLoader.instance().getRouter().routes());\n        this._initialized = true;\n\n        return Promise.resolve();\n    }\n\n    public start(): void {\n        if (!this._initialized) {\n            return;\n        }\n\n        const host = '0.0.0.0';\n        const port = '8081';\n        this.app.listen(port, host, () =\u003e {\n            console.log(`server start, Address: ${host}:${port}!`);\n        });\n    }\n}\n\n\nconst server = new Server();\nserver.init(process.env.NODE_ENV === 'development')\n    .then(() =\u003e {\n        server.start();\n    })\n    .catch((error) =\u003e {\n        console.log(`Gateway init failed error = ${error.stack}`);\n    });\n\nprocess.on('uncaughtException', (error) =\u003e {\n    console.log(`process on uncaughtException error = ${error.stack}`);\n});\n\nprocess.on('unhandledRejection', (error) =\u003e {\n    console.log(`process on unhandledRejection error = ${error.stack}`);\n});\n```\n\n### Test API Gateway\n\n```bash\n# api \ncurl -d \"isbn=1\" \"http://127.0.0.1:8081/v1/getBookUser\"\n\n# mock api when SET NODE_ENV=development\ncurl -d \"isbn=1\" \"http://127.0.0.1:8081/v1/getBookUser?mock=1\"\n```\n\n## Tool Chain\n\n- [protoc-gen-grpc](https://github.com/niklaus0823/protoc-gen-grpc)\n- [matrixes-cli](https://github.com/niklaus0823/matrixes-cli)\n- [matrixes-lib](https://github.com/niklaus0823/matrixes-lib)\n\n## Simple\n\n- [matrixes-simple](https://github.com/niklaus0823/matrixes-simple)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniklaus0823%2Fmatrixes-lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fniklaus0823%2Fmatrixes-lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniklaus0823%2Fmatrixes-lib/lists"}