koa-thrift
v1.1.0
Published
The thrift middle of Koa.
Downloads
15
Readme
koa-thrift
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
more node.js examples from official
License
MIT