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

1.1.0 • Public • Published

koa-thrift

NPM Version Node Version

The thrift middle of Koa, foundation for koaland

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));
}

Generate code

install thrift binary on macOS with brew

cd ./examples
thrift -version  # Thrift version 0.13.0
thrift -r --gen js:node unpkg.thrift

Examples

examples with client are listed at examples

Others

Thrift Missing Guide

more node.js examples from official

License

MIT

Dependencies (4)

Dev Dependencies (2)

Package Sidebar

Install

npm i koa-thrift

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

15.4 kB

Total Files

6

Last publish

Collaborators

  • cooperhsiung