Build function-based API with minimal code
- Basic
- @tomrpc/core 最核心的函数机制
- @tomrpc/client 客户端,支持Node.js和浏览器
- Advance
- @tomrpc/mount 自动加载某目录下面的所有文件,如果export的是函数,则通过rpc.fn加入。
- Application(Todo)
- @tomrpc/app 组合core和mount,增加内置中间件、环境判断等,用于应用创建。
- 内置cors、serve、view、jwt中间件
Server 函数,app.js
import { createServer } from '@tomrpc/core';
const rpc = createServer({
fn: {
// prefix: '/apk2',
},
});
rpc.fn('/a', function (a) {
return { a: a };
});
rpc.start(2091);
execute app.js and start server with port 2091
$ node app.js
Client client.js
import { createClient } from '@tomrpc/client'
const client = createClient({
host: '127.0.0.1',
port: 3000,
namespace: '',
});
const res = await client.a('hello');
console.dir(res);
实际上,执行
http://127.0.0.1:3000/api/a?$p=[%22hello%22]
返回的结果
{a: 'hello'}
rpc.fn('a', function (a: string) {
return { a: a };
});
server
rpc.fn('com.yourcompony.a', function (a: string) {
return a;
});
client
import { createClient } from '@tomrpc/client';
const client = createClient({
host: '127.0.0.1',
port: 3000,
namespace: 'com.yourcompony',
});
const res = await client.a('hello');
console.dir(res);
this === koa ctx
rpc.fn('com.yourcompany.a', function (a: string) {
return return `${this.path} , ${a} `;
});
server
rpc.fn('com.yourcompany.a', function (a: string) {
if (this.method === 'post'){
return return `${this.path} , ${a} `;
}
});
client
import { createClient } from '@tomrpc/client';
const client = createClient({
host: '127.0.0.1',
port: 3000,
namespace: 'com.yourcompany',
methodFilter: function (lastKey: string) {
if (lastKey === 'a') {
return 'post';
} else {
return 'get';
}
},
});
const res = await client.a('hello');
console.dir(res);
import { join } from 'desm';
import { createApp } from '@tomcat/app';
const rpc = createApp({
name: 'tomapp',
base: import.meta.url,
port: 3001,
debug: false,
mount: './f',
buildin: {
serve: {
enable: true, root: join(import.meta.url, '.', 'public'), opts: {}
},
cors: { enable: true },
view: {
enable: true,
root: join(import.meta.url, '.', 'view'),
opts: {
map: {
html: 'ejs',
},
},
},
},
});
rpc.fn('a', function (a) {
return { a: a };
});
rpc.start();
在core里,存在4个生命周期,每个都是middleware数组。
- init: []
- before: []
- load: []
- after: []
- default: Middlewaire
import { createServer } from '@tomrpc/core';
const rpc = createServer({
default: async (ctx) => {
ctx.body = 'custom default'
}
});
rpc.init.push(someMiddleware)
MIT @ npmstudy