{"id":13716060,"url":"https://github.com/nickduncan7/micro-http-router","last_synced_at":"2025-05-07T05:32:06.808Z","repository":{"id":57296435,"uuid":"102155895","full_name":"nickduncan7/micro-http-router","owner":"nickduncan7","description":"simple, Express-like router for micro","archived":false,"fork":false,"pushed_at":"2019-05-21T14:01:36.000Z","size":233,"stargazers_count":24,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-18T23:45:06.396Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/nickduncan7.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":"2017-09-01T21:55:33.000Z","updated_at":"2021-09-13T15:29:46.000Z","dependencies_parsed_at":"2022-09-07T03:20:56.665Z","dependency_job_id":null,"html_url":"https://github.com/nickduncan7/micro-http-router","commit_stats":null,"previous_names":["protocol114/micro-http-router"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickduncan7%2Fmicro-http-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickduncan7%2Fmicro-http-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickduncan7%2Fmicro-http-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickduncan7%2Fmicro-http-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nickduncan7","download_url":"https://codeload.github.com/nickduncan7/micro-http-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252782552,"owners_count":21803401,"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-08-03T00:01:06.638Z","updated_at":"2025-05-07T05:32:03.110Z","avatar_url":"https://github.com/nickduncan7.png","language":"JavaScript","funding_links":[],"categories":["Modules"],"sub_categories":["Routing"],"readme":"# micro-http-router\n[![Build Status](https://travis-ci.org/protocol114/micro-http-router.svg?branch=master)](https://travis-ci.org/protocol114/micro-http-router) [![Coverage Status](https://coveralls.io/repos/github/protocol114/micro-http-router/badge.svg?branch=master)](https://coveralls.io/github/protocol114/micro-http-router?branch=master)\n\nmicro-http-router is a simple, Express-like router for [micro](https://github.com/zeit/micro) that uses a radix tree via [radix-router](https://github.com/charlieduong94/radix-router). It supports most, if not all, of the HTTP verbs, and tries to be as lightweight and quick as possible.\n\n## Installation\nmicro-http-router is an npm package and you should have the latest Node.js and npm installed.\n\n```bash\n\u003e npm i --save micro-http-router\n```\n\nhttps://www.npmjs.com/package/micro-http-router\n\n## Usage\n\n```javascript\nconst micro = require('micro');\nconst Router = require('micro-http-router');\n\n// Initialize the router\nconst router = new Router();\n\n// Define a basic GET request\nrouter.route({\n    path: '/',\n    method: 'GET',\n    handler: (req, res) =\u003e {\n        return 'Hello, world';\n    }\n});\n\n// Define a basic GET request with a middleware function\nrouter.route({\n    path: '/',\n    method: 'GET',\n    before: (req, res) =\u003e {\n        req.user = {};\n        req.user.name = 'John Doe';\n    },\n    handler: (req, res) =\u003e {\n        return `Hello, ${ req.user.name }`;\n    }\n});\n\n// Express-like routing helpers\nrouter.get('/', (req, res) =\u003e {\n    return 'Hello, world';\n});\n\n// Async body parsing\nrouter.post('/', async (req, res) =\u003e {\n    const body = await micro.json(req);\n    return body;\n});\n\n// Any number of route parameters are supported\n// Access via the req.params array\nrouter.get('/:first/:second', (req, res) =\u003e {\n    const first = req.params.first;\n    const second = req.params.second;\n    return `Your first parameter is ${ first } and your second is ${ second }.`;\n});\n\n// Start micro and listen\nconst server = micro((req, res) =\u003e router.handle(req, res));\nconst port = 3000;\nserver.listen(port);\nconsole.log(`micro is listening on port: ${ port }`);\n```\n\n## Changelog (1.3.0 =\u003e 1.5.1)\n\n##### New in 1.5.1\nFixed some potential issues with the unrouting functionality. Upgraded dependencies to remove potential vulnerabilities.\n\n##### New in 1.5.0\nAdded support for query parameters using WHATWG URL API. The WHATWG URL `.searchParams` object gets transplanted onto the `request` object as `.searchParams` as well for a familiar API for retrieving and manipulating those query parameters.\n\nAlso added `unroute(path, method)` function to the router that can be called to delete route handlers, if necessary. It can be used in this manner:\n\n```javascript\n// Configure a basic get route\nrouter.get('/', (req, res) =\u003e {\n    ...\n});\n\n// don't want the route? call router.unroute(path, method)!\nrouter.unroute('/',  'GET');\n\n// Now subsequent calls to that route and method will fail, as it no longer exists. 👍\n```\n\nYou can also remove all configured routes by calling `router.unrouteAll()`, and you can see configured routes and what methods are configured by accessing `router.routes`.\n\n```javascript\nconst router = new Router();\n\n// Configure the routes\nrouter.get('/foo', (req, res) =\u003e {\n    ...\n});\nrouter.post('/foo', (req, res) =\u003e {\n    ...\n});\nrouter.get('/bar', (req, res) =\u003e {\n    ...\n});\n\nconsole.log(router.routes);\n// Returns:\n{ '/foo': [ 'GET', 'POST' ], '/bar': [ 'GET' ] }\n```\n\n##### New in 1.3.0\nUpdated dependencies, including radix-router. The only major change is that in place of a request parameter array, the parameters are added to the `req.params` object in a named fashion. If you define a route as `'/:id'`, instead of using `req.params[0]` to get the passed in ID, you can simply use `req.params.id`. Additionally, merged in [PR #3](https://github.com/protocol114/micro-http-router/pull/3) which adds a full stack trace to the error output when `debug` is set to `true`.\n\n##### New in 1.2.0\nAdd `debug: true` to the router options object to enable debug logging. Any thrown errors will have their error messages returned as the response body. Useful when developing with micro-http-router and you are unexpectedly receiving 500 Internal Server Errors.\n\n##### New in 1.1.0\nAdded `before` option to route options. This serves as a single layer of middleware allowing you to do the following:\n\n```javascript\nconst micro = require('micro');\nconst Router = require('micro-http-router');\n\nconst beforeHandler = function (req, res) {\n    req.user = {};\n    req.user.name = 'John Doe';\n};\n\n// Initialize the router\nconst router = new Router();\n\n// Define a basic GET request with a middleware function\nrouter.route({\n    path: '/',\n    method: 'GET',\n    before: beforeHandler,\n    handler: (req, res) =\u003e {\n        return `Hello, ${ req.user.name }`;\n    }\n});\n\n// or define it with your get shorthand\nrouter.get('/', beforeHandler, (req, res) =\u003e {\n    return `Hello, ${ req.user.name }`;\n});\n\n// Start micro and listen\nconst server = micro((req, res) =\u003e router.handle(req, res));\nconst port = 3000;\nserver.listen(port);\nconsole.log(`micro is listening on port: ${ port }`);\n```\n\n## Tests\n`micro-http-router` comes with a few [AVA](https://github.com/avajs/ava) tests to help ensure correct routing behavior.\n\nTo execute the tests yourself, simply run:\n\n`npm test`\n\n## License\nMIT License\n\nCopyright (c) 2017 Nick Duncan\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.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickduncan7%2Fmicro-http-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnickduncan7%2Fmicro-http-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickduncan7%2Fmicro-http-router/lists"}