{"id":18814954,"url":"https://github.com/simplymichael/node-laravel-router","last_synced_at":"2026-02-26T20:11:22.502Z","repository":{"id":243042795,"uuid":"811300704","full_name":"simplymichael/node-laravel-router","owner":"simplymichael","description":"A Laravel-inspired router for Node.js","archived":false,"fork":false,"pushed_at":"2024-08-19T01:58:36.000Z","size":142,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-01T02:55:52.223Z","etag":null,"topics":["express","laravel","node","nodejs","router","routing","routing-engine"],"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/simplymichael.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-06T10:31:39.000Z","updated_at":"2024-08-19T01:56:46.000Z","dependencies_parsed_at":"2024-06-06T22:49:33.772Z","dependency_job_id":"8ba15a0f-980d-4a77-b127-addbccc2bea0","html_url":"https://github.com/simplymichael/node-laravel-router","commit_stats":null,"previous_names":["simplymichael/express-router-laravel","simplymichael/node-laravel-router"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/simplymichael/node-laravel-router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplymichael%2Fnode-laravel-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplymichael%2Fnode-laravel-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplymichael%2Fnode-laravel-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplymichael%2Fnode-laravel-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simplymichael","download_url":"https://codeload.github.com/simplymichael/node-laravel-router/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplymichael%2Fnode-laravel-router/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29870767,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T18:42:30.764Z","status":"ssl_error","status_checked_at":"2026-02-26T18:41:47.936Z","response_time":89,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["express","laravel","node","nodejs","router","routing","routing-engine"],"created_at":"2024-11-07T23:47:09.407Z","updated_at":"2026-02-26T20:11:22.486Z","avatar_url":"https://github.com/simplymichael.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# node-laravel-router\nA Laravel-inspired router for node.js apps.\n\n(Forked from [Express Laravel Router](https://github.com/shaunpersad/express-laravel-router))\n\n[![NPM version][npm-version-image]][npm-url]\n[![Node version][node-version-image]][node-url]\n[![NPM Downloads][npm-downloads-image]][package-url]\n[![License][license-image]][license-url]\n[![Conventional commits][conventional-commits-image]][conventional-commits-url]\n[![Tests][ci-image]][ci-url]\n[![Coverage][codecov-image]][codecov-url]\n\n## Motivation\nThis router is an alternative to routers such as the one that ships with express.js.\nInstead of manually creating instances of `express.Router`, for example,\nyou can define your routes in group closures,\nwhere it becomes easier to create and reason about the shared properties of your routes.\n\nAlso, this router allows you to execute custom code for each route definition, which can be useful for many things,\ne.g. injecting dependencies into each request handler, or [automatically creating a swagger/openapi spec from your routes](https://github.com/simplymichael/node-laravel-router/wiki/Swagger-spec-generation-example).\n\nThere are also some extra features like being able to name and generate urls strings for each route.\n\nTo summarize:\n- Easily create and organize route groups\n- Execute custom code for each route definition\n- Generate urls for a given route definition\n\n## Installation\n```bash\nnpm install node-laravel-router --save\n```\n\n## Quickstart\n\nThe examples below will create two routes:\n1. A GET to `/api/v1/users/{userId}`\n2. A POST to `/api/v1/auth`\n\n### Express app example\n```js\nconst express = require('express');\nconst createRouter = require('node-laravel-router').createRouter;\n\nconst app = express();\nconst router = createRouter(app);\n\nrouter.group('/api', (router) =\u003e {\n    router.group('/v1', (router) =\u003e {\n        router.group('/users', (router) =\u003e {\n            router.get('/{userId}', (req, res) =\u003e { /* request handler logic */ });    \n        });\n\n        router.post('/auth', (req, res) =\u003e { /* request handler logic */ });\n    });\n});\n```\n\nTo create the above example in pure express.js, it would look something like the following:\n```js\nconst express = require('express');\n\nconst app = express();\nconst apiRouter = express.Router();\nconst v1Router = express.Router();\nconst usersRouter = express.Router();\n\nusersRouter.get('/:userId', (req, res) =\u003e { /* request handler logic */ });\n\nv1Router.use('/users', usersRouter);\nv1Router.get('/auth', (req, res) =\u003e { /* request handler logic */ });\n\napiRouter.use('/v1', v1Router);\n\napp.use('/api', apiRouter);\n```\nThe pure express.js version is not only visually harder to reason about,\nbut it becomes increasingly more complex as more routes and middleware are added.\n\n\n### Generic Node app example\n```js\nconst createRouter = require('node-laravel-router').createRouter;\nconst router = createRouter();\n\nrouter.group('/api', (router) =\u003e {\n    router.group('/v1', (router) =\u003e {\n        router.group('/users', (router) =\u003e {\n            router.get('/{userId}', (req, res) =\u003e { /* request handler logic */ });    \n        });\n\n        router.post('/auth', (req, res) =\u003e { /* request handler logic */ });\n    });\n});\n\n// When we pass an Express (or Express-type) app, like in the Express app example above,\n// routing is automatically applied once we call the router's methods.\n// With our generic, non-Express app, however, routing is not automatically.\n// To apply the routing, we have to call the router's apply method as follows.\n// Let's assume we are using an Express app:\nrouter.apply((route) =\u003e {\n  // Do something with route.\n  // For example, assuming we have a connect-middleware-supported app\n  // stored in an `app` variable:\n  const { method, path, handlers } = route;\n\n  app[method](path, handlers);\n});\n```\n\n## Usage\nOur quickstart example used strings as the first argument for both the `router.group` and `router.get` methods. You are\nalso able to supply an `options` object instead, for more powerful functionality. Supplying just a string is actually a\nshortcut to setting the `prefix` option in the group `options` object, and the `uri` option in the route `options` object.\n\n### Group options\nThe full group `options` object with their default values looks like this:\n```js\n{\n    \"prefix\": \"/\", // url prefix shared by all routes in this group\n    \"middleware\": [], // middleware shared by all routes in this group\n    \"namespace\": \"\", // namespace shared by all named routes in this group\n    \"patterns\": {}, // regex patterns shared by all route params in this group\n    \"meta\": {} // additional meta data to associate to all routes in this group\n}\n```\nNote that all fields are optional, and any combination of fields can be used.\n\nAlso note that the following are all equivalent:\n```js\nrouter.group({ prefix: '/api' }, (router) =\u003e {\n\n});\n\n// shortcut to the above\nrouter.group('/api', (router) =\u003e {\n\n});\n```\nFor more details, see the [wiki page](https://github.com/simplymichael/node-laravel-router/wiki/Group-options).\n\n### Route options\nThe full route `options` object with their default values looks like this:\n```js\n{\n    \"method\": \"get\", // the HTTP method for this route definition\n    \"uri\": \"/\", // the url fragment for this route definition\n    \"middleware\": [], // the middleware specific to this route definition\n    \"name\": \"\", // a name to associate to this route definition\n    \"patterns\": {}, // any patterns specific to this route definition\n    \"meta\": {} // any additional meta data to associate to this route definition\n}\n```\nNote that all fields are optional, and any combination of fields can be used.\n\nAlso note that the following are all equivalent:\n```js\nrouter.route({ method: 'get', uri: '/api' }, (req, res) =\u003e {\n\n});\n\n// the default method is \"get\"\nrouter.route({ uri: '/api' }, (req, res) =\u003e {\n\n});\n\n// the default method is \"get\", and if a string is used instead of an object, that string becomes the uri option.\nrouter.route('/api', (req, res) =\u003e {\n\n});\n\n// using router.{method} prefills the options object with the correct method.\nrouter.get({ uri: '/api' }, (req, res) =\u003e {\n\n});\n\n// shortcut to the above\nrouter.get('/api', (req, res) =\u003e {\n\n});\n```\nFor more details, see the [wiki page](https://github.com/simplymichael/node-laravel-router/wiki/Route-options).\n\n## Full API\nBelow are all the methods available on a `router`.\n\n##### `router.group(options|prefix, closure)`\nCreates a route group and provides a new `router` instance inside the `closure` to perform additional routing inside that group.\n\n##### `router.route(options|uri, action)`\nCreates a route definition and associates an `action` with it. The `action` is then passed to a `mapActionToHandler`\nfunction, whose job it is to return the familiar express.js requestHandler function, whose signature is:\n```js\n(req, res) =\u003e {}\n```\nThe default `mapActionToHandler` function assumes that the `action` passed to `router.route` is already an express.js\nrequestHandler function. This behavior can be changed by supplying a new `mapActionToHandler` function to `createRouter`:\n```js\nconst mapActionToHandler = (action, routeDescription, routeOptions) =\u003e {\n\n    return action.customHandler;\n};\n\nconst router = createRouter(app, mapActionToHandler);\n\nrouter.route('/users', { customHandler: (req, res) =\u003e {} });\n```\nThe above example now expects that the `action` is an object with a `customHandler` property.\n\n##### `router.{method}(options|uri, action)`\nInstead of supplying a `method` in the options of `router.route`, you can simply call `router.{method}`, which will\nset the proper `method` field in the options.\n```js\nrouter.route({ method: 'post', uri: '/create' }, (req, res) =\u003e {\n\n});\n\n// shortcut to the above\nrouter.post('/create', (req, res) =\u003e {\n\n});\n```\n\n##### `router.serve(uri, staticMiddleware)`\nCreates a route that serves static files.\n```js\nrouter.serve('/assets', express.static('./public/assets'));\n```\n\n##### `router.url(name, params={}, options={})`\nCreates and returns a url for the route definition with the given name. If that route contains params, you can pass in\nvalues to fill in the params in the optional `params` object. Any extra fields found in the `params` object but not found\nin the route definition will be considered query params, and will be appended to the url as a query string.\n\nQuery strings are generated via the [qs](https://www.npmjs.com/package/qs) module, and so any options that you'd like to\npass to `qs.stringify` can be done via the optional `options` object.\n\nIf there are any patterns on the params, they must be honored by the supplied params, or an error will be thrown.\n```js\nrouter.group({ prefix: '/user/{userId}', namespace: 'user.' }, (router) =\u003e {\n\n    router.get({ uri: '/friend/{friendId}', name: 'getFriend' }, (req, res) =\u003e {\n\n    });\n});\n\nconst url = router.url('user.getFriend', { userId: 1, friendId: 2, foo: 'bar' });\n// url will equal /user/1/friend/2?foo=bar\n```\n```js\nrouter.get({\n    uri: '/user/{userId}',\n    name: 'getUser',\n    patterns: {\n        userId: /^\\d+$/\n    }\n}, (req, res) =\u003e {\n\n});\nconst url = router.url('getUser', { userId: \"one\"}); // this will throw an error, because userId is expected to be a number.\n```\n\n##### `router.app`\nGrants access to the express `app` object that was passed in to `createRouter`.\n\n## Extras\nIn addition to the `createRouter` function, this package also exports `laravelToExpress` and `uriWithParams` functions.\n\n##### `laravelToExpress(uri = '', patterns = {})`\nAccepts a string uri written in the Laravel way (e.g. `/user/{userId}`) and an optional object of regex patterns,\nand returns the express.js version (e.g. `/user/:userId`).\n\n##### `uriWithParams(uri = '', params = {}, patterns = {}, options = {})`\nAccepts a string uri with optional params (e.g. `/user/{userId}`). If that uri contains params, you can pass in\nvalues to fill in the params in the optional `params` object. Any extra fields found in the `params` object but not found\nin the route definition will be considered query params, and will be appended to the url as a query string.\n\nQuery strings are generated via the [qs](https://www.npmjs.com/package/qs) module, and so any options that you'd like to\npass to `qs.stringify` can be done via the optional `options` object.\n\nIf there are any patterns on the params, they must be honored by the supplied params, or an error will be thrown.\n\n##### `paramsFromUri(uri = '')`\nAccepts a string uri and will return an object that includes and array of required and optional params found in the uri.\n```js\nconst uri = '/users/{userId}/friends/{friendId}/{username?}';\nconst params = paramsFromUri(uri);\n/**\n* params looks like:\n* { required: [\"userId\", \"friendId\"], optional: [\"username\"] }\n*/\n\n```\n\n## Differences to Laravel\nUnlike Laravel routes, chaining is discarded in favor of objects containing options. I found this to be a much clearer API.\n\nAdditionally, some of Laravel's naming has been updated or repurposed for clarity, e.g. Laravel's \"as\" has become \"name\"\nin route definitions, and \"namespace\" in route groups.\n\n\n[npm-url]: https://npmjs.com/package/node-laravel-router\n[npm-version-image]: https://img.shields.io/npm/v/node-laravel-router\n[node-url]: https://nodejs.org/\n[node-version-image]: https://img.shields.io/node/v/node-laravel-router\n[package-url]: https://npm.im/node-laravel-router\n[npm-downloads-image]: https://img.shields.io/npm/dm/node-laravel-router\n[license-url]: https://github.com/simplymichael/node-laravel-router/blob/master/LICENSE.md\n[license-image]: https://img.shields.io/github/license/simplymichael/node-laravel-router\n[conventional-commits-url]: https://conventionalcommits.org\n[conventional-commits-image]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-brightgreen.svg\n[ci-url]: https://github.com/simplymichael/node-laravel-router/actions/workflows/run-coverage-tests.yml\n[ci-image]: https://github.com/simplymichael/node-laravel-router/workflows/tests/badge.svg\n[codecov-url]: https://codecov.io/gh/simplymichael/node-laravel-router\n[codecov-image]: https://img.shields.io/codecov/c/github/simplymichael/node-laravel-router?token=9NNHXS8HDU\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplymichael%2Fnode-laravel-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimplymichael%2Fnode-laravel-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplymichael%2Fnode-laravel-router/lists"}