{"id":16846625,"url":"https://github.com/othree/node-r3","last_synced_at":"2025-03-17T05:32:04.257Z","repository":{"id":57159028,"uuid":"20711628","full_name":"othree/node-r3","owner":"othree","description":"r3 binding on node.js","archived":false,"fork":false,"pushed_at":"2014-10-02T10:14:16.000Z","size":461,"stargazers_count":95,"open_issues_count":0,"forks_count":5,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-04-25T11:22:08.359Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.org/package/node-r3","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/othree.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":"2014-06-11T04:01:54.000Z","updated_at":"2020-06-18T16:39:21.000Z","dependencies_parsed_at":"2022-09-08T09:02:22.129Z","dependency_job_id":null,"html_url":"https://github.com/othree/node-r3","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othree%2Fnode-r3","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othree%2Fnode-r3/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othree%2Fnode-r3/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othree%2Fnode-r3/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/othree","download_url":"https://codeload.github.com/othree/node-r3/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243846976,"owners_count":20357294,"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-10-13T13:03:58.999Z","updated_at":"2025-03-17T05:32:03.776Z","avatar_url":"https://github.com/othree.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"node-r3\n=======\n\nNode.js [r3][r3] binding.\n\n[![Build Status](https://travis-ci.org/othree/node-r3.svg?branch=master)](https://travis-ci.org/othree/node-r3)\n\nInstall\n-------\n\nCompile and install [r3][r3] follow its readme. Then `npm install node-r3`.\n\nOn Mac OSX, using homebrew:\n\n    brew install r3\n\nUsage\n-----\n\nBasic usage:\n\n```js\nvar Router = require('node-r3').Router;\n\nvar router = new Router({\n  \"/foo\": \"data string\",\n  \"/foo/bar\": function () {},\n  \"/foo/bar/qoo\": {obj: 1},\n});\n\nvar dispatched = router.match(\"/foo\");\n\nrouter.free();\n```\n\nThe router's initial argument is an POJSO(Plain Old JavaScript Object). Key is route path and value is data. It is possible to add method condition in route, ex: `GET /foo`. And all JS data type can be used for data. Method condition can support 3 format.\n\n1. Single method, ex: `GET`, `POST`.\n2. Multiple method. Both `,` and `|` can be used as separator. Ex: `GET,POST` or `POST|PUT`.  \n   No space allowed.\n3. Integer in string, ex: `1`, `4`. Usefule for custom router application. Don't support multiple method in this format. You should deal with it before send to Router. Ex: `[1 | 4, '/foo'].join(' ')`\n\nThere is a http handler helper function:\n\n```js\nvar router = new Router({\n  \"/\": handler,\n  \"/foo\": fooHandler,\n  \"/foo/{id}\": fooHandler,\n  \"POST /me\": postMeHandler,\n  \"GET /me\": getMeHandler,\n  \"POST|GET /post\": postHandler,\n});\n\nvar server = http.createServer(router.httpHandler(notfound));\n```\n\nIf the data is a function. It will auto execute when route match. And receive `[req, res, params...]`. Otherwise, it will call `notfound` as fallback. Arguments will be `[req, res, data, params...]`. A sample file `sample/http.js` is provided.\n\nPath and Router\n---------------\n\nThere are two router method on r3. One is path, one is route. The paths function is very basic. No condition, only string routing. The route  is much powerful. Supports methods condition. So its the default one in node-r3. If you want to use path router as default. Try `var Router = require('node-r3').PathRouter`. It still possible to use route function in a PathRouter. User `insert_route` and `match_route` instead of the default `insert` and `match` method. Remember to recompile the Router before use it.\n\nAPI\n---\n\n\u0010\u0010\u0010Constructor:\n\n* `Router(router config)`\n* `PathRouter(router config)`\n\nRouter methods:\n\n* `compile() -\u003e void`\n* `dump() -\u003e void`\n* `free() -\u003e void`\n* `insert(route or path, data) -\u003e void`\n* `insert_route(route, data) -\u003e void`\n* `insert_path(path, data) -\u003e void`\n* `match(route or path) -\u003e [data, [captures]]`\n* `match_route(route) -\u003e [data, [captures]]`\n* `match_path(path) -\u003e [data, [captures]]`\n* `httpHandler(handler) -\u003e function`\n\nPath is just a string, route is more powerful, format:\n\n    \"#{METHODS} #{PATH}\"\n\nMethods formats:\n\n    \"METHOD\"\n    \"METHOD1|METHOD2\"\n    \"METHOD1,METHOD2\"\n    \"3\"\n\nMethod includes:\n\n* `GET`\n* `POST`\n* `PUT`\n* `DELETE`\n* `PATCH`\n* `HEAD`\n* `OPTIONS`\n\nRouter config:\n\n    {\n        \"#{ROUTE1}\": data1,\n        \"#{ROUTE2}\": data2,\n        \"#{ROUTE3}\": data3,\n    }\n\nData can be any JavaScript data type. If data is function. When use `httpHandler`. It will auto execute data function when matched. The arguments:\n\n    dataFunction(req, res, captures...)\n    \nIf data is not function, `httpHandler` will execute `handler` function. And with following arguments:\n\n    handler(req, res, data, captures...)\n    \nIn `httpHandler`, when no route matches. `handler` will will execute also. But without data and captures.\n\n    handler(req, res)\n    \n\nAlternative\n-----------\n\nThere is another [caasi/node-r3][] projetc use different approach to let node can use r3's feature\n\n[caasi/node-r3]:https://github.com/caasi/node-r3\n\nTODO\n----\n\n* Solve memory leak.\n\n[r3]:https://github.com/c9s/r3\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fothree%2Fnode-r3","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fothree%2Fnode-r3","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fothree%2Fnode-r3/lists"}