koa-thrift
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.4 • Public • Published

    koa-thrift

    NPM Version Node Version

    The thrift middle of Koa.

    Installation

    npm i koa-thrift -S

    Usage

    • simple app
    var UnpkgService = require('./gen-nodejs/UnpkgService');
    import { Context } from 'koa';
    import KoaThrift from 'koa-thrift';
    
    const app = new KoaThrift({ service: UnpkgService });
    
    // not found
    app.use((ctx: Context) => {
      console.log('ctx.request: ', ctx.request);
    });
    
    // result -> InvalidOperation TApplicationException: Not Found
    
    app.listen(9090);
    console.log('listening on 9090...');
    • with route
    var UnpkgService = require('./gen-nodejs/UnpkgService');
    const mount = require('koa-mount');
    import { Context } from 'koa';
    import KoaThrift from 'koa-thrift';
    
    const app = new KoaThrift({ service: UnpkgService });
    
    // with route
    app.use(
      mount('/Publish', function (ctx: Context) {
        console.log('ctx.request: ', ctx.request);
        ctx.body = { code: 0, message: 'publish success' };
      })
    );
    
    app.listen(9090);
    console.log('listening on 9090...');
    • use middleware
    var UnpkgService = require('./gen-nodejs/UnpkgService');
    const mount = require('koa-mount');
    import { Context } from 'koa';
    import KoaThrift from 'koa-thrift';
    
    const app = new KoaThrift({ service: UnpkgService });
    
    // use middleware
    app.use(async (ctx, next) => {
      const start = Date.now();
      await next();
      console.log( `process ${ctx.path} request from ${ctx.ip} cost ${Date.now() - start}ms`);
    });
    
    // with route
    app.use(
      mount('/Publish', async (ctx: Context) => {
        console.log('ctx.request: ', ctx.request);
        await sleep(300);
        ctx.body = { code: 0, message: 'publish success' };
      })
    );
    
    app.listen(9090);
    console.log('listening on 9090...');
    
    function sleep(delay = 1000) {
      return new Promise((resolve) => setTimeout(resolve, delay));
    }

    Examples

    examples with client are listed at examples

    Todo

    • [ ] middleware
    • [ ] wrap client

    Others

    Thrift Missing Guide

    License

    MIT

    Keywords

    Install

    npm i koa-thrift

    DownloadsWeekly Downloads

    2

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    15 kB

    Total Files

    6

    Last publish

    Collaborators

    • cooperhsiung