@medomino-170/koa-server

0.0.21 • Public • Published

快速生成 nodejs(koa) 后端服务

示例

import { createServer, API, RouteItem } from "../src";

interface ExtensKey {
  body: {
    __token: {
      user: string;
    };
  };
  headers: {
    token: string;
  };
  extend: {
    compress: boolean;
  };
}

type Base = API<ExtensKey>;
type WrapRoute<T extends Record<string, any> = {}> = RouteItem<ExtensKey, T>;

const get: WrapRoute<{ lql: string }> = {
  method: "get",
  async callback({ body: { __token, lql }, ctx }) {
    console.log(__token, lql, ctx.query, ctx.search, ctx.request.body);
    return 123;
  },
};

const api: Base["api"] = {
  "/api": {
    "/v/": get,
  },
};

const intercept: Base["intercept"] = {
  "/api/(.*)": async (_ctx, next) => {
    await next();
  },
};

createServer({
  base: {
    port: 8000,
  },
  api: {
    api,
    intercept,
  },
});

Readme

Keywords

none

Package Sidebar

Install

npm i @medomino-170/koa-server

Weekly Downloads

1

Version

0.0.21

License

MIT

Unpacked Size

8.38 kB

Total Files

14

Last publish

Collaborators

  • liqiling