@leizm/connect
TypeScript icon, indicating that this package has built-in type declarations

1.7.0 • Public • Published

NPM version build status Test coverage David deps node version npm download npm license

@leizm/connect

Greenkeeper badge

现代的 Web 中间件基础框架,完美支持 TypeScript,构建可维护的大型 Web 项目。

本框架参考了 connect、express 和 koa 等主流框架,具有以下特点:

  • 兼容 connect 中间件,可以通过内置的函数转换 connect 中间件,使用 NPM 上大量的模块资源
  • 可将本框架的实例转换为 connect 中间件,与其他项目模块紧密合作
  • 简单没有歧义的接口参数,内置 TypeScript 支持
  • 更好的性能

安装

npm install @leizm/connect --save

基本使用方法

import { Connect, Router } from "@leizm/connect";

const app = new Connect();
const router = new Router();

// 基本的中间件
app.use("/", function(ctx) {
  console.log("hello, world");
  ctx.next();
});

// 支持 async function
app.use("/", async function(ctx) {
  consoole.log("async function");
  await sleep(1000);
  ctx.next();
});

// 路由中间件
router.get("/hello/:a/:b", function(ctx) {
  console.log("a=%s, b=%s", ctx.request.params.a, ctx.request.params.b);
  ctx.response.html("it works");
});
app.use("/", router);

// 错误处理
app.use("/", function(ctx, err) {
  ctx.response.json({ message: err.message });
});

// 监听端口
app.listen({ port: 3000 }, () => {
  console.log("server started");
});

扩展

扩展 Request 与 Response 对象的方法:参考单元测试程序

模板文件 connect.ts(自己的项目中引用此文件中的 ConnectRouter,而不是来自 @leizm/connect 的):

import * as base from "@leizm/connect";

export type MiddlewareHandle = (
  ctx: Context,
  err?: base.ErrorReason
) => Promise<void> | void;

export class Connect extends base.Connect<Context> {
  protected contextConstructor: base.ContextConstructor = Context;
}

export class Router extends base.Router<Context> {
  protected contextConstructor: base.ContextConstructor = Context;
}

export class Context extends base.Context<Request, Response> {
  protected requestConstructor: base.RequestConstructor = Request;
  protected responseConstructor: base.ResponseConstructor = Response;
}

export class Request extends base.Request {}

export class Response extends base.Response {
  // 扩展方法
  public ok(data: any) {
    this.json({ data });
  }
  public error(error: message) {
    this.json({ error });
  }
}

性能

性能测试程序 结果(性能优于主流框架 koaexpress.js):

------------------------------------------------------------------------
connection: close 方式请求:

26882.24 Requests/sec - ukoa.js
14892.25 Requests/sec - ufeathers.js
13657.59 Requests/sec - uexpress.js
5853.36 Requests/sec - micro.js
5040.39 Requests/sec - leizm-connect.js
5002.74 Requests/sec - rawnode.js
4279.85 Requests/sec - koa.js
3784.59 Requests/sec - total/total.js
3509.53 Requests/sec - express.js
3469.05 Requests/sec - feathers.js
3025.51 Requests/sec - restify.js
1261.24 Requests/sec - hapi.js
1097.77 Requests/sec - sails/sails.js
156.60 Requests/sec - uws.js

------------------------------------------------------------------------
connection: keep-alive 方式请求:

31348.44 Requests/sec - uws.js
27884.30 Requests/sec - ukoa.js
14129.84 Requests/sec - uexpress.js
14047.04 Requests/sec - ufeathers.js
13352.23 Requests/sec - micro.js
11644.37 Requests/sec - rawnode.js
11088.41 Requests/sec - leizm-connect.js
8334.33 Requests/sec - koa.js
7588.25 Requests/sec - total/total.js
6026.16 Requests/sec - express.js
5370.37 Requests/sec - restify.js
3443.71 Requests/sec - feathers.js
1617.57 Requests/sec - hapi.js
1508.43 Requests/sec - sails/sails.js

授权协议

MIT License

Copyright (c) 2017 老雷 <leizongmin@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Readme

Keywords

Package Sidebar

Install

npm i @leizm/connect

Weekly Downloads

1

Version

1.7.0

License

MIT

Unpacked Size

75 kB

Total Files

30

Last publish

Collaborators

  • leizongmin