现代的 Web 中间件基础框架,完美支持 TypeScript,构建可维护的大型 Web 项目
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
test_data/template
.coveralls.yml
.gitignore
.prettierrc.js
.travis.yml
LICENSE
README.md
package.json
tsconfig.json
tsconfig.typedoc.json

README.md

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

@leizm/web

Greenkeeper badge DeepScan grade

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

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

  • 兼容 connect 中间件,可以通过内置的函数转换 connect 中间件,使用 NPM 上大量的模块资源
  • 可将本框架的实例转换为 connect 中间件,与其他项目模块紧密合作
  • 支持直接操作原生 req 和 res 对象
  • 简单没有歧义的接口参数,内置 TypeScript 支持,强大的代码自动提示支持
  • 内置路由功能及众多常用的中间件,无需借助第三方模块
  • 性能由于主流框架 koa 和 express
  • 代码库轻盈,依赖模块少

内置中间件列表:

  • bodyParser 请求体解析:
    • json 解析 application/json,基于模块body-parser
    • text 解析 text/plain,基于模块 body-parser
    • urlencoded 解析 application/x-www-form-urlencoded,基于模块 body-parser
    • raw 解析 application/octet-stream,基于模块 body-parser
    • multipart 解析 multipart/form-data ,基于模块 busboy
  • cookieParser 解析 Cookie,基于模块 cookie-parser
  • serveStatic 静态文件服务,基于模块 serve-static
  • cors 设置 CORS
  • session 提供多存储引擎的 Session 支持:
    • SessiionMemoryStore 内存存储引擎
    • SessiionRedisStore Redis 存储引擎,通过传入 Redis 客户端实例实现存储,支持 ioredisredis 模块
    • SimpleRedisClientOptions 简单 Redis 客户端,可以不依赖第三方模块的情况下实现 Redis 存储,直接在 SessionRedisStore 初始化时指定 { host, port, db } 来代替 client 参数即可

详细使用说明可阅读 Wiki

安装

npm i @leizm/web -S

Hello, world

import * as web from "@leizm/web";

// 创建app实例
const app = new web.Application();
// 快速初始化 ejs 模板,需要手动安装 ejs 模块
app.templateEngine.initEjs();

app.router.get("/a", async function(ctx) {
  // 渲染模板,模板文件为 views/index.html
  ctx.response.render("index", { msg: "hello, world" });
});

app.router.get("/b", async function(ctx) {
  // 返回JSON
  ctx.response.json({ msg: "hello, world" });
});

// 监听端口
app.listen({ port: 3000 });

基本使用方法

import { Application, Router, component } from "@leizm/web";

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

// 使用内置中间件
app.use("/", component.cookieParser());

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

// 支持 async function
app.use("/", async function(ctx) {
  console.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 对象的方法:参考单元测试程序

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

import * as base from "@leizm/web";
export * from "@leizm/web";

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

export class Application extends base.Application<Context> {
  protected contextConstructor = Context;
}

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

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

export class Request extends base.Request {
  // 扩展 Request
  public get remoteIP() {
    return String(this.req.headers["x-real-ip"] || this.req.headers["x-forwarded-for"] || this.req.socket.remoteAddress);
  }
}

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

性能

性能测试程序 结果(性能优于主流框架 koa 的 13%,express 的 43%):

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

8390.82 Requests/sec - restify.js
6983.61 Requests/sec - micro.js
6905.93 Requests/sec - leizm-web.js
6578.84 Requests/sec - http.js
6415.45 Requests/sec - rawnode.js
5414.55 Requests/sec - koa.js
5263.68 Requests/sec - total/total.js
4926.65 Requests/sec - feathers.js
4829.53 Requests/sec - express.js
4401.81 Requests/sec - hapi.js
1640.84 Requests/sec - sails/sails.js

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

12279.14 Requests/sec - restify.js
11630.27 Requests/sec - micro.js
11584.45 Requests/sec - leizm-web.js
11298.82 Requests/sec - http.js
10730.96 Requests/sec - rawnode.js
9415.69 Requests/sec - koa.js
9178.14 Requests/sec - total/total.js
7024.93 Requests/sec - express.js
6038.25 Requests/sec - hapi.js
4772.14 Requests/sec - feathers.js
1640.60 Requests/sec - sails/sails.js

授权协议

MIT License

Copyright (c) 2017-2018 老雷 <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.