{"id":28472880,"url":"https://github.com/fromsko/hono-server","last_synced_at":"2025-07-02T01:30:40.373Z","repository":{"id":289991695,"uuid":"973049539","full_name":"Fromsko/hono-server","owner":"Fromsko","description":"hono-server for api","archived":false,"fork":false,"pushed_at":"2025-04-26T06:53:31.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-07T11:34:50.914Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Fromsko.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,"zenodo":null}},"created_at":"2025-04-26T06:30:50.000Z","updated_at":"2025-04-26T06:53:35.000Z","dependencies_parsed_at":"2025-04-26T07:38:35.691Z","dependency_job_id":null,"html_url":"https://github.com/Fromsko/hono-server","commit_stats":null,"previous_names":["fromsko/hono-server"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Fromsko/hono-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fromsko%2Fhono-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fromsko%2Fhono-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fromsko%2Fhono-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fromsko%2Fhono-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Fromsko","download_url":"https://codeload.github.com/Fromsko/hono-server/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fromsko%2Fhono-server/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263059473,"owners_count":23407366,"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":"2025-06-07T11:30:39.631Z","updated_at":"2025-07-02T01:30:40.365Z","avatar_url":"https://github.com/Fromsko.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hono-server\nhono-server for api\n\n## api-server\n\n```ts\nimport { Hono } from \"hono\";\nimport { basicAuth } from \"hono/basic-auth\";\nimport { bearerAuth } from \"hono/bearer-auth\";\nimport { bodyLimit } from \"hono/body-limit\";\nimport { cors } from \"hono/cors\";\nimport { csrf } from \"hono/csrf\";\nimport { logger } from \"hono/logger\";\nimport { compress } from \"hono/compress\";\nimport { prettyJSON } from \"hono/pretty-json\";\nimport { RegExpRouter } from \"hono/router/reg-exp-router\";\n\nconst app = new Hono({ router: new RegExpRouter() });\nconst token = \"honoiscool\";\n\napp.use(csrf());\napp.use(logger());\napp.use(compress());\napp.use(prettyJSON());\n\n// CORS should be called before the route\napp.use(\"/api/*\", bearerAuth({ token }));\napp.use(\n  \"/api2/*\",\n  cors({\n    origin: \"http://example.com\",\n    allowHeaders: [\"X-Custom-Header\", \"Upgrade-Insecure-Requests\"],\n    allowMethods: [\"POST\", \"GET\", \"OPTIONS\"],\n    exposeHeaders: [\"Content-Length\", \"X-Kuma-Revision\"],\n    maxAge: 600,\n    credentials: true,\n  })\n);\n\napp.all(\"/api/abc\", (c) =\u003e {\n  return c.json({ success: true });\n});\napp.all(\"/api2/abc\", (c) =\u003e {\n  return c.json({ success: true });\n});\napp.use(\n  \"/auth/*\",\n  basicAuth({\n    username: \"hono\",\n    password: \"acoolproject\",\n  })\n);\n\napp.post(\n  \"/upload\",\n  bodyLimit({\n    maxSize: 50 * 1024, // 50kb\n    onError: (c) =\u003e {\n      return c.text(\"超出限制 :(\", 413);\n    },\n  }),\n  async (c) =\u003e {\n    const body = await c.req.parseBody();\n    if (body[\"file\"] instanceof File) {\n      console.log(`收到文件大小：${body[\"file\"].size}`);\n    }\n    return c.text(\"pass\");\n  }\n);\n\napp.get(\"/api\", (c) =\u003e {\n  return c.json({\n    code: 1,\n    msg: \"Hello World!\",\n    data: {},\n  });\n});\n\napp.post(\"/api/page\", bearerAuth({ token }), (c) =\u003e {\n  return c.json({ message: \"Created post!\" }, 201);\n});\n\napp.get(\"/auth/page\", (c) =\u003e {\n  return c.text(\"You are authorized\");\n});\n\napp.delete(\n  \"/auth/page\",\n  basicAuth({\n    verifyUser: (username, password, c) =\u003e {\n      return username === \"dynamic-user\" \u0026\u0026 password === \"hono-password\";\n    },\n  }),\n  (c) =\u003e {\n    return c.text(\"Page deleted\");\n  }\n);\n\nconst readToken = \"read\";\nconst privilegedToken = \"read+write\";\nconst privilegedMethods = [\"POST\", \"PUT\", \"PATCH\", \"DELETE\"];\n\napp.on(\"GET\", \"/api/page/*\", async (c, next) =\u003e {\n  // 有效令牌列表\n  const bearer = bearerAuth({ token: [readToken, privilegedToken] });\n  return bearer(c, next);\n});\napp.on(privilegedMethods, \"/api/page/*\", async (c, next) =\u003e {\n  // 单个有效特权令牌\n  const bearer = bearerAuth({ token: privilegedToken });\n  return bearer(c, next);\n});\n\napp\n  .get(\"/\", (c) =\u003e c.text(\"Hi\"))\n  .post(\"/json\", (c) =\u003e {\n    return c.req.json();\n  })\n  .get(\"/id/:id\", (c) =\u003e {\n    const id = c.req.param(\"id\");\n    const name = c.req.query(\"name\");\n\n    c.header(\"x-powered-by\", \"benchmark\");\n\n    return c.text(`${id} ${name}`);\n  });\n\nexport default app;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffromsko%2Fhono-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffromsko%2Fhono-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffromsko%2Fhono-server/lists"}