{"id":18834556,"url":"https://github.com/tphp/xweb","last_synced_at":"2026-01-26T12:30:16.247Z","repository":{"id":57402498,"uuid":"403539686","full_name":"tphp/xweb","owner":"tphp","description":"koa-web快捷调用","archived":false,"fork":false,"pushed_at":"2021-09-07T11:08:40.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T22:37:34.779Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/tphp.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}},"created_at":"2021-09-06T08:14:28.000Z","updated_at":"2021-09-07T11:08:37.000Z","dependencies_parsed_at":"2022-09-17T06:51:36.438Z","dependency_job_id":null,"html_url":"https://github.com/tphp/xweb","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tphp%2Fxweb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tphp%2Fxweb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tphp%2Fxweb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tphp%2Fxweb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tphp","download_url":"https://codeload.github.com/tphp/xweb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239768923,"owners_count":19693764,"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":[],"created_at":"2024-11-08T02:13:09.336Z","updated_at":"2026-01-26T12:30:16.192Z","avatar_url":"https://github.com/tphp.png","language":"JavaScript","readme":"## 使用说明\n\n- 使用基本步骤请参考: [koa-web](https://www.npmjs.com/package/koa-web)\n- 新增session的支持，[koa-session](https://www.npmjs.com/package/koa-session) 数据保存到根目录的.cache中\n- koa-session默认保存到cookie中，这种情况并不安全\n- 但koa-session也提供了数据保存接口，xweb简单的封装了下\n- xweb启动时将自动清理过期的session数据文件\n\n#### 调试 xweb\n\n```\nnpx xweb\n\n无需安装访问：http://localhost:3000 就可以看到效果\n```\n\n#### 安装 xweb\n\n```\nnpm i xweb\n```\n\n#### 启动程序\n\n```js\nconst Xweb = require(\"xweb\");\n\nconst web = new Xweb();\n\n// 应用于 koa-web的配置 app.use(KoaWeb({}));\n// 参考: https://www.npmjs.com/package/koa-web\n// 除 sessionKey 和 sessionMaxAge 外\nweb.config({\n  // session 将保存到 __dirname/.cache中\n  path: __dirname,\n\n  // session文件保存路径\n  // sessionPath: __dirname,\n\n  // cookie键名 默认: xweb\n  // sessionKey: \"xweb\",\n\n  // cookie过期时间， 默认 86400000 (一天)\n  // sessionMaxAge: 86400000\n});\n\n// // 调用于: koa.use\n// web.use(async (ctx, next) =\u003e {\n//   await next();\n//   ctx.body = 'xweb';\n// });\n\n// 调用于: koa.listen\nweb.listen(3000, () =\u003e {\n  console.log(\"server is running at http://localhost:3000\");\n});\n```\n\n---\n\n## session调用实例\n\n#### 创建html页面: /html/session/login.html\n```html\n\u003chtml\u003e\n\u003chead\u003e\n  \u003ctitle\u003e{% if login %}欢迎: {{ username }}{% else %}登录测试{% endif %}\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n{% if login %}\n  \u003cdiv\u003e欢迎: {{ username }}\u003c/div\u003e\n  \u003cform action=\"login.logout\" method=\"post\"\u003e\n    \u003cdiv\u003e\u003cinput type=\"hidden\" name=\"logout\" value=\"true\"\u003e\u003c/div\u003e\n    \u003cdiv\u003e\u003cinput type=\"submit\" value=\"退出\"\u003e\u003c/div\u003e\n  \u003c/form\u003e\n{% else %}\n  \u003cform action=\"login.login\" method=\"post\"\u003e\n    \u003cdiv\u003e\u003cinput type=\"text\" name=\"username\"\u003e\u003c/div\u003e\n    \u003cdiv\u003e\u003cinput type=\"text\" name=\"password\"\u003e\u003c/div\u003e\n    \u003cdiv\u003e\u003cinput type=\"submit\" value=\"登录\"\u003e\u003c/div\u003e\n  \u003c/form\u003e\n{% endif %}\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n#### 创建数据控制页面: /html/session/login.js\n\n```js\n// html登录页面\nmodule.exports.html = async hd =\u003e {\n  if (hd.ctx.session.login) {\n    hd.view({\n      login: hd.ctx.session.login,\n      username: hd.ctx.session.username\n    });\n  } else {\n    hd.view({\n      login: false\n    });\n  }\n};\n\n// 用户登录 账号: admin 密码: admin\nmodule.exports.login = async (hd, data) =\u003e {\n  if (!hd.isPost()) {\n    return \"数据提交错误!\";\n  }\n\n  let username = data.username;\n  let password = data.password;\n\n  if (username !== 'admin' || password !== 'admin') {\n    return \"登录失败\";\n  }\n\n  hd.ctx.session.login = true;\n  hd.ctx.session.username = username;\n\n  hd.ctx.status = 301;\n  hd.ctx.redirect('/session/login');\n\n  return \"登录成功\";\n};\n\n// 用户登出\nmodule.exports.logout = async (hd, data) =\u003e {\n  if (data.logout === 'true') {\n    hd.ctx.session = null;\n  }\n\n  hd.ctx.status = 301;\n  hd.ctx.redirect('/session/login');\n};\n```\n\n- 访问: http://localhost:3000/session/login","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftphp%2Fxweb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftphp%2Fxweb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftphp%2Fxweb/lists"}