{"id":13673265,"url":"https://github.com/syumai/dinatra","last_synced_at":"2025-04-06T02:08:51.925Z","repository":{"id":41570827,"uuid":"159836619","full_name":"syumai/dinatra","owner":"syumai","description":"Sinatra like light weight web app framework for deno.","archived":false,"fork":false,"pushed_at":"2021-12-31T12:49:47.000Z","size":167,"stargazers_count":416,"open_issues_count":8,"forks_count":17,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-30T01:06:32.611Z","etag":null,"topics":["deno","typescript","webserver"],"latest_commit_sha":null,"homepage":"","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/syumai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-30T14:42:38.000Z","updated_at":"2025-01-11T01:09:24.000Z","dependencies_parsed_at":"2022-08-10T02:50:37.695Z","dependency_job_id":null,"html_url":"https://github.com/syumai/dinatra","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syumai%2Fdinatra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syumai%2Fdinatra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syumai%2Fdinatra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syumai%2Fdinatra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syumai","download_url":"https://codeload.github.com/syumai/dinatra/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247423515,"owners_count":20936626,"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":["deno","typescript","webserver"],"created_at":"2024-08-02T10:00:32.228Z","updated_at":"2025-04-06T02:08:51.908Z","avatar_url":"https://github.com/syumai.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","Uncategorized","Modules","基础设施","typescript","Libraries"],"sub_categories":["Uncategorized","Online Playgrounds","Assistants","Deno 源"],"readme":"# dinatra\n\n[![Build Status](https://github.com/syumai/dinatra/workflows/test/badge.svg?branch=master)](https://github.com/syumai/dinatra/actions)\n\n- [Sinatra](http://sinatrarb.com/) like light weight web app framework for [Deno](https://github.com/denoland/deno).\n- This app is using [Deno Standard Modules](https://github.com/denoland/deno_std).\n- **All features of this app are currently experimental**.\n\n## Usage\n\n`example/index.ts`\n\n```ts\nimport {\n  app,\n  get,\n  post,\n  redirect,\n  contentType,\n} from \"https://denopkg.com/syumai/dinatra@0.15.0/mod.ts\";\n\napp(\n  get(\"/hello\", () =\u003e \"hello\"),\n  get(\"/hello/:id\", ({ params }) =\u003e params.id),\n  get(\n    \"/hello/:id/and/:name\",\n    ({ params }) =\u003e `:id is ${params.id}, :name is ${params.name}`,\n  ),\n  get(\"/error\", () =\u003e [500, \"an error has occured\"]),\n  get(\"/callName\", ({ params }) =\u003e `Hi, ${params.name}!`),\n  post(\"/callName\", ({ params }) =\u003e `Hi, ${params.name}!`),\n  get(\"/foo\", () =\u003e redirect(\"/hello\", 302)), // redirect from /foo to /hello\n  get(\"/info\", () =\u003e [\n    200,\n    contentType(\"json\"),\n    JSON.stringify({ app: \"dinatra\", version: \"0.0.1\" }),\n  ]),\n);\n```\n\n```console\ndeno run --allow-net --allow-read index.ts # Or simply: deno run -A index.ts\n# App runs on localhost:8080\n\ncurl http://localhost:8080/hello\n# status: 200\n# body: hello\n\ncurl http://localhost:8080/hello/1\n# status: 200\n# body: 1\n\ncurl http://localhost:8080/hello/1/and/John\n# status: 200\n# body: :id is 1, :name is John\n\ncurl http://localhost:8080/error\n# status: 500\n# body: an error has occured\n\ncurl http://localhost:8080/callName?name=John\n# status: 200\n# body: Hi, John!\n\ncurl -d 'name=Tom' http://localhost:8080/callName\n# status: 200\n# body: Hi, Tom!\n\ncurl http://localhost:8080/foo\n# status: 302\n# location: /hello\n\ncurl http://localhost:8080/info\n# status: 200\n# content-type: application/json\n# body: {\"app\":\"dinatra\",\"version\":\"0.0.1\"}\n```\n\n### Async Handler\n\n- You can use async function as handler.\n\n[`example/template/index.ts`](https://github.com/syumai/dinatra/tree/master/example/template)\n\n```ts\nconst { cwd, open } = Deno;\nimport { app, get } from 'https://denopkg.com/syumai/dinatra/mod.ts';\n\nconst currentDir = cwd();\nconst htmlPath = `${currentDir}/index.html`;\n\napp(get('/', async () =\u003e await open(htmlPath)));\n```\n\n### Template\n\n- You can use [dejs](https://github.com/syumai/dejs) (ejs for deno) as dinatra's template engine.\n\n```ts\nimport { renderFile } from 'https://deno.land/x/dejs/dejs.ts';\n\napp(\n  get('/', async () =\u003e await renderFile('index.ejs', { message: 'example' }))\n);\n```\n\n### Host static files\n\n- Requires `--allow-read`, e.g. `deno run --allow-read --allow-net app.ts`\n- Files in `./public` directory will be served static.\n\n### Close server\n\n```ts\nimport { app, get } from 'https://denopkg.com/syumai/dinatra/mod.ts';\n\nconst s = app(get('/', () =\u003e 'hello'));\n\nsetTimeout(() =\u003e {\n  s.close(); // close server after 5s.\n}, 5000);\n```\n\n## Flags\n\n```console\ndeno run -A index.ts -p 8000 # or --port=8000\n# App runs on localhost:8000\n```\n\n## Initialization options\n\n### Customize static file hosting option\n\n```ts\nimport { defaultPort } from 'https://denopkg.com/syumai/dinatra/constants.ts';\nimport { App, get } from 'https://denopkg.com/syumai/dinatra/mod.ts';\n\nconst app = new App(\n  defaultPort, // portNumber (number)\n  'dist', // public file directory's path (string)\n  false // option to enable static file hosting (boolean)\n);\n\napp.register(get('/hello', () =\u003e 'hello'));\n```\n\n## Response Types\n\n```ts\n// HeaderMap is a type of response headers.\ntype HeaderMap =\n  | Headers\n  | {\n      [key: string]: any;\n    };\n\n// ResponseBody is a type of response body.\ntype ResponseBody = string | ReadCloser | Deno.Reader;\n\n/*\n *  Types of Response\n */\n\n// StatusHeadersBodyResponse is a response with status code, headers, body.\ntype StatusHeadersBodyResponse = [number, HeaderMap, ResponseBody];\n\n// StatusBodyResponse is a response with status code, body.\ntype StatusBodyResponse = [number, ResponseBody];\n\n// Response is a type of response.\nexport type Response =\n  | StatusHeadersBodyResponse\n  | StatusBodyResponse\n  | number // HTTP status code only\n  | ResponseBody; // Response body only\n\n// Response interface\ninterface HTTPResponse {\n  status?: number;\n  headers?: Headers;\n  body?: Uint8Array | Deno.ReadCloser | Deno.Reader;\n}\n```\n\n## Status\n\n### Request Params\n\n- [x] URL query params (for GET)\n- [x] route params (like: `/users/:user_id/posts`)\n- [x] x-www-form-urlencoded\n- [x] redirect\n- [x] application/json\n- [ ] application/octet-stream\n\n## Development\n\n### Update module\n\n- Please use [dem](https://github.com/syumai/dem)\n\n```\ndem update https://deno.land/std@v0.xx.x\n```\n\n### Lint\n\n- `make lint`\n\n### Format\n\n- `make fmt`\n\n### Testing\n\n- `make test`\n\n## Author\n\nsyumai\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyumai%2Fdinatra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyumai%2Fdinatra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyumai%2Fdinatra/lists"}