{"id":15975792,"url":"https://github.com/JBlaak/Express-Torch","last_synced_at":"2025-10-20T15:31:29.011Z","repository":{"id":76232504,"uuid":"72287011","full_name":"JBlaak/Express-Torch","owner":"JBlaak","description":"Expressive and maintainable routes for Express.js","archived":false,"fork":false,"pushed_at":"2018-03-22T10:12:10.000Z","size":145,"stargazers_count":51,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-03T04:22:29.335Z","etag":null,"topics":["express","routing"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JBlaak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2016-10-29T13:39:14.000Z","updated_at":"2023-05-04T17:24:15.000Z","dependencies_parsed_at":"2023-03-11T22:00:42.398Z","dependency_job_id":null,"html_url":"https://github.com/JBlaak/Express-Torch","commit_stats":null,"previous_names":["jblaak/torch"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlaak%2FExpress-Torch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlaak%2FExpress-Torch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlaak%2FExpress-Torch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlaak%2FExpress-Torch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JBlaak","download_url":"https://codeload.github.com/JBlaak/Express-Torch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237358349,"owners_count":19297059,"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":["express","routing"],"created_at":"2024-10-07T22:05:11.164Z","updated_at":"2025-10-20T15:31:28.656Z","avatar_url":"https://github.com/JBlaak.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Express Torch\n=====\n\n[![Build Status](https://travis-ci.org/JBlaak/Express-Torch.svg?branch=master)](https://travis-ci.org/JBlaak/Express-Torch)\n\nExpressive and maintainable routes for Express.js.\n\nRunning the example\n-----\n\nThe project includes a simple example rendering a `/` and `/api/posts` endpoint,\nyou can set it up as follows:\n\n```bash\n$ cd ./example\n$ yarn\n$ yarn start\n```\n\nNow visit `http://localhost:3000` and take a look at `server.js`!\n\nUsage\n-----\n\n__Basic usage__\n\nYou pass your Express app to Torch, and a callback in which you can \nspecify your routes.\n\n```js\nimport Express from 'express';\nimport Torch from 'express-torch';\nimport HomeController from './controllers/home';\n\nconst app = Express();\n\nTorch(app, (router) =\u003e {\n    router.get('/', HomeController.index);\n});\n\n//... your other Express logic\n\napp.listen(3000);\n```\n\n__Grouping__\n\nThis is where Torch shines, you can register a bunch of routes\nthat require all parameters of the groups above, such as a `prefix` for\nyour `/api` routes.\n\n```js\nimport Express from 'express';\nimport Torch from 'express-torch';\nimport HomeController from './controllers/home';\n\nconst app = Express();\n\nTorch(app, (router) =\u003e {\n    router.get('/', HomeController.index);\n    router.group({prefix: 'api'}, (router) =\u003e {\n        router.get('/posts', HomeController.index);// will evaluate to /api/posts\n        router.get('/posts/:id', HomeController.show);// will evaluate to /api/posts/:id\n    });\n});\n\n//... your other Express logic\n\napp.listen(3000);\n```\n\n__Middleware__\n\nGroups have another advantage, you can apply regular Express middleware to them,\nso that they stack together without you having to specify them per route\n\n```js\nimport Express from 'express';\nimport Torch from 'express-torch';\nimport HomeController from './controllers/home';\n\nconst app = Express();\n\nTorch(app, (router) =\u003e {\n    router.group({middlware: [Cookies, Session]}, (router) =\u003e {\n    router.get('/', HomeController.index);// Cookies \u003e Session\n    router.group({middleware: [Throttle]}, (router) =\u003e {\n        router.get('/posts', HomeController.index);// Cookies \u003e Session \u003e Throttle\n        router.group({middleware: [OAuth, Policy('manage-posts')]}, (router) =\u003e {\n            router.get('/posts/:id', HomeController.show);// Cookies \u003e Session \u003e Throttle \u003e OAuth \u003e Policy('manage-posts')\n        });\n    });\n});\n\n//... your other Express logic\n\napp.listen(3000);\n```\n\nif you want, you can still apply middleware to a single route\n\n```js\nimport Express from 'express';\nimport Torch from 'express-torch';\nimport HomeController from './controllers/home';\n\nconst app = Express();\n\nTorch(app, (router) =\u003e {\n    router.get('/', {,\n        middleware: [Auth],\n        controller: HomeController.index\n    });\n});\n\n//... your other Express logic\n\napp.listen(3000);\n```\n\n__Naming__\n\nSince urls can be subject to your client's requests you don't want to spread\nthem all around your application as if they were static. Torch allows you to\nadd a mapping so that some `name` will map to a path.\n\n```js\nimport Express from 'express';\nimport Torch from 'express-torch';\nimport PostsController from './controllers/posts';\n\nconst app = Express();\n\nconst routes = Torch(app, (router) =\u003e {\n    router.group({prefix: '/api'}, function(router) {\n        router.get('/posts/:id', {,\n            name: 'api.posts.show',\n            controller: PostsController.show\n        });\n    });\n});\n\nroutes.named('api.posts.show',  { id: 123 });// will evaluate to /api/posts/123\n\n//... your other Express logic\n\napp.listen(3000);\n\n/**** in ./controllers/posts.js ****/\n\n{\n    index: (req, res) =\u003e {\n        req.routes.named('api.posts.show', {id: 123}) \n    }\n}\n```\n\n__Constraints__\n\nMost of the time we want to constrain our routes their variables, for example an `:id` should in some applications only match a number. Express allows us to use regex inside a route path but those make the route look bloated and hard to read. To still be able to constrain our routes but still keep them readable Torch allows us to define the constraints in an extra method.\n\n```js\nimport Express from 'express';\nimport Torch from 'express-torch';\nimport PostsController from './controllers/posts';\n\nconst app = Express();\n\nconst routes = Torch(app, (router) =\u003e {\n    router.group({prefix: '/api'}, function(router) {\n        router.get('/posts/:id', PostsController.show).where({'id', '(\\\\d+)'}); // will evaluate to /api/posts/:id(\\\\d+)\n    });\n});\n\n//... your other Express logic\n\napp.listen(3000);\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJBlaak%2FExpress-Torch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJBlaak%2FExpress-Torch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJBlaak%2FExpress-Torch/lists"}