{"id":18083430,"url":"https://github.com/leizongmin/leizm-web","last_synced_at":"2025-04-10T10:12:50.970Z","repository":{"id":48624946,"uuid":"99693001","full_name":"leizongmin/leizm-web","owner":"leizongmin","description":"现代的 Web 中间件基础框架，完美支持 TypeScript，构建可维护的大型 Web 项目","archived":false,"fork":false,"pushed_at":"2024-09-21T06:52:16.000Z","size":1231,"stargazers_count":43,"open_issues_count":3,"forks_count":11,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-24T09:04:12.545Z","etag":null,"topics":["connect","express","framework","koa","nodejs","web"],"latest_commit_sha":null,"homepage":"http://morning.work","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leizongmin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-08T12:58:35.000Z","updated_at":"2024-12-20T02:07:34.000Z","dependencies_parsed_at":"2024-11-24T19:05:42.007Z","dependency_job_id":"17fe7383-cb6d-4515-83bf-529f906600ca","html_url":"https://github.com/leizongmin/leizm-web","commit_stats":{"total_commits":319,"total_committers":4,"mean_commits":79.75,"dds":"0.047021943573667735","last_synced_commit":"d5c960bb5b77f00e7a2f4e4ea92f52d55ded9702"},"previous_names":["leizongmin/leizm-connect"],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fleizm-web","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fleizm-web/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fleizm-web/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fleizm-web/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leizongmin","download_url":"https://codeload.github.com/leizongmin/leizm-web/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248198888,"owners_count":21063628,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["connect","express","framework","koa","nodejs","web"],"created_at":"2024-10-31T14:07:57.984Z","updated_at":"2025-04-10T10:12:50.943Z","avatar_url":"https://github.com/leizongmin.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM version][npm-image]][npm-url]\n![Node.js CI](https://github.com/leizongmin/leizm-web/workflows/Node.js%20CI/badge.svg)\n[![DeepScan grade](https://deepscan.io/api/projects/2695/branches/18968/badge/grade.svg)](https://deepscan.io/dashboard#view=project\u0026pid=2695\u0026bid=18968)\n[![Test coverage][coveralls-image]][coveralls-url]\n[![David deps][david-image]][david-url]\n[![node version][node-image]][node-url]\n[![npm download][download-image]][download-url]\n[![npm license][license-image]][download-url]\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fleizongmin%2Fleizm-web.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fleizongmin%2Fleizm-web?ref=badge_shield)\n\n[npm-image]: https://img.shields.io/npm/v/@leizm/web.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@leizm/web\n[coveralls-image]: https://img.shields.io/coveralls/leizongmin/leizm-web.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/leizongmin/leizm-web?branch=master\n[david-image]: https://img.shields.io/david/leizongmin/leizm-web.svg?style=flat-square\n[david-url]: https://david-dm.org/leizongmin/leizm-web\n[node-image]: https://img.shields.io/badge/node.js-%3E=_10-green.svg?style=flat-square\n[node-url]: http://nodejs.org/download/\n[download-image]: https://img.shields.io/npm/dm/@leizm/web.svg?style=flat-square\n[download-url]: https://npmjs.org/package/@leizm/web\n[license-image]: https://img.shields.io/npm/l/@leizm/web.svg\n\n# @leizm/web\n\n现代的 Web 中间件基础框架，完美支持 TypeScript，构建可维护的大型 Web 项目。\n\n本框架参考了 connect、express 和 koa 等主流框架，具有以下特点：\n\n* 兼容 connect 中间件，可以通过内置的函数转换 connect 中间件，使用 NPM 上大量的模块资源\n* 可将本框架的实例转换为 connect 中间件，与其他项目模块紧密合作\n* 支持直接操作原生 req 和 res 对象\n* 简单没有歧义的接口参数，内置 TypeScript 支持，强大的代码自动提示支持\n* 内置路由功能及众多常用的中间件，无需借助第三方模块\n* 性能优于主流框架 Koa 和 Express\n* 代码库轻盈，依赖模块少\n\n-----\n\n内置中间件列表：\n\n* `bodyParser` 请求体解析：\n  * `json` 解析 application/json，基于模块[body-parser](https://www.npmjs.com/package/body-parser)\n  * `text` 解析 text/plain，基于模块 [body-parser](https://www.npmjs.com/package/body-parser)\n  * `urlencoded` 解析 application/x-www-form-urlencoded，基于模块 [body-parser](https://www.npmjs.com/package/body-parser)\n  * `raw` 解析 application/octet-stream，基于模块 [body-parser](https://www.npmjs.com/package/body-parser)\n  * `multipart` 解析 multipart/form-data ，基于模块 [busboy](https://www.npmjs.com/package/busboy)\n* `cookieParser` 解析 Cookie，基于模块 [cookie-parser](https://www.npmjs.com/package/cookie-parser)\n* `serveStatic` 静态文件服务，基于模块 [serve-static](https://www.npmjs.com/package/serve-static)\n* `favicon` Favicon中间件，使用方法类似于 [serve-favicon](https://github.com/expressjs/serve-favicon)\n* `cors` 设置 CORS\n* `session` 提供多存储引擎的 Session 支持：\n  * `SessionMemoryStore` 内存存储引擎\n  * `SessionRedisStore` Redis 存储引擎，通过传入 Redis 客户端实例实现存储，支持 [ioredis](https://www.npmjs.com/package/ioredis) 和 [redis](https://www.npmjs.com/package/redis) 模块\n  * `SimpleRedisClientOptions` 简单 Redis 客户端，可以不依赖第三方模块的情况下实现 Redis 存储，直接在 `SessionRedisStore` 初始化时指定 `{ host, port, db }` 来代替 `client` 参数即可\n\n**🌟🌟🌟🌟详细使用说明可阅读 [Wiki](https://github.com/leizongmin/leizm-web/wiki)🌟🌟🌟🌟**\n\n## 安装\n\n```bash\nnpm i @leizm/web -S\n```\n\n## Hello, world\n\n```typescript\nimport * as web from \"@leizm/web\";\n\n// 创建app实例\nconst app = new web.Application();\n// 快速初始化 ejs 模板，需要手动安装 ejs 模块\napp.templateEngine.initEjs();\n\napp.router.get(\"/a\", async function(ctx) {\n  // 渲染模板，模板文件为 views/index.html\n  ctx.response.render(\"index\", { msg: \"hello, world\" });\n});\n\napp.router.get(\"/b\", async function(ctx) {\n  // 返回JSON\n  ctx.response.json({ msg: \"hello, world\" });\n});\n\n// 监听端口\napp.listen({ port: 3000 });\n```\n\n## 基本使用方法\n\n```typescript\nimport * as web from \"@leizm/web\";\n\nconst app = new web.Application();\nconst router = new web.Router();\n\n// 使用内置中间件\napp.use(\"/\", web.component.cookieParser());\n\n// 基本的中间件\napp.use(\"/\", function(ctx) {\n  console.log(\"hello, world\");\n  ctx.next();\n});\n\n// 支持 async function\napp.use(\"/\", async function(ctx) {\n  console.log(\"async function\");\n  await sleep(1000);\n  ctx.next();\n});\n\n// 路由中间件\nrouter.get(\"/hello/:a/:b\", function(ctx) {\n  console.log(\"a=%s, b=%s\", ctx.request.params.a, ctx.request.params.b);\n  ctx.response.html(\"it works\");\n});\napp.use(\"/\", router);\n\n// 错误处理\napp.use(\"/\", function(ctx, err) {\n  ctx.response.json({ message: err.message });\n});\n\n// 监听端口\napp.listen({ port: 3000 }, () =\u003e {\n  console.log(\"server started\");\n});\n```\n\n## 扩展\n\n扩展 Request 与 Response 对象的方法：[参考单元测试程序](https://github.com/leizongmin/leizm-web/blob/master/src/test/extends.test.ts)\n\n模板文件 `web.ts`（自己的项目中引用此文件中的 `Application` 和 `Router`，而不是来自 `@leizm/web` 的）：\n\n```typescript\nimport * as base from \"@leizm/web\";\nexport * from \"@leizm/web\";\n\nexport type MiddlewareHandle = (ctx: Context, err?: base.ErrorReason) =\u003e Promise\u003cvoid\u003e | void;\n\nexport class Application extends base.Application\u003cContext\u003e {\n  protected contextConstructor = Context;\n}\n\nexport class Router extends base.Router\u003cContext\u003e {\n  protected contextConstructor = Context;\n}\n\nexport class Context extends base.Context\u003cRequest, Response\u003e {\n  protected requestConstructor = Request;\n  protected responseConstructor = Response;\n}\n\nexport class Request extends base.Request {\n  // 扩展 Request\n  public get remoteIP() {\n    return String(this.req.headers[\"x-real-ip\"] || this.req.headers[\"x-forwarded-for\"] || this.req.socket.remoteAddress);\n  }\n}\n\nexport class Response extends base.Response {\n  // 扩展 Response\n  public ok(data: any) {\n    this.json({ data });\n  }\n  public error(error: string) {\n    this.json({ error });\n  }\n}\n```\n\n## 性能\n\n[性能测试程序](https://github.com/leizongmin/leizm-web-benchmark) 结果（性能略低于主流框架 **koa** 的 -5%，高于 **express** 的 +235%）：\n\n------------------------------------------------------------------------\n\n### connection: close 方式请求：\n\n- 8370 Requests/sec - micro.js\n- 8185 Requests/sec - http.js\n- **7612 Requests/sec - koa.js**\n- **7302 Requests/sec - @leizm/web🌟🌟**\n- 5871 Requests/sec - restify.js\n- 5800 Requests/sec - hapi.js\n- **3602 Requests/sec - express.js**\n\n------------------------------------------------------------------------\n\n### connection: keep-alive 方式请求：\n\n- 22780 Requests/sec - http.js\n- 18899 Requests/sec - micro.js\n- **17704 Requests/sec - koa.js**\n- **16793 Requests/sec - @leizm/web🌟🌟**\n- 11603 Requests/sec - restify.js\n- 11428 Requests/sec - hapi.js\n- **5012 Requests/sec - express.js**\n\n\n## 授权协议\n\n```text\nMIT License\n\nCopyright (c) 2017-2021 老雷 \u003cleizongmin@gmail.com\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n\n\n## License\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fleizongmin%2Fleizm-web.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fleizongmin%2Fleizm-web?ref=badge_large)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleizongmin%2Fleizm-web","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleizongmin%2Fleizm-web","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleizongmin%2Fleizm-web/lists"}