{"id":13624760,"url":"https://github.com/aichbauer/express-routes-mapper","last_synced_at":"2025-04-12T11:54:50.150Z","repository":{"id":18401315,"uuid":"84200658","full_name":"aichbauer/express-routes-mapper","owner":"aichbauer","description":"a small mapper for express routes","archived":false,"fork":false,"pushed_at":"2022-12-09T17:11:12.000Z","size":852,"stargazers_count":21,"open_issues_count":26,"forks_count":16,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-04T20:40:51.489Z","etag":null,"topics":["dynamic-routes","express","mapper","nodejs","routes"],"latest_commit_sha":null,"homepage":null,"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/aichbauer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-03-07T13:12:32.000Z","updated_at":"2023-03-09T02:36:22.000Z","dependencies_parsed_at":"2023-01-11T20:28:57.776Z","dependency_job_id":null,"html_url":"https://github.com/aichbauer/express-routes-mapper","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aichbauer%2Fexpress-routes-mapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aichbauer%2Fexpress-routes-mapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aichbauer%2Fexpress-routes-mapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aichbauer%2Fexpress-routes-mapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aichbauer","download_url":"https://codeload.github.com/aichbauer/express-routes-mapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248565051,"owners_count":21125415,"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":["dynamic-routes","express","mapper","nodejs","routes"],"created_at":"2024-08-01T21:01:46.008Z","updated_at":"2025-04-12T11:54:50.118Z","avatar_url":"https://github.com/aichbauer.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# express-routes-mapper\n\n[![Build Status](https://travis-ci.org/aichbauer/express-routes-mapper.svg?branch=master)](https://travis-ci.org/aichbauer/express-routes-mapper) [![Coverage Status](https://coveralls.io/repos/github/aichbauer/express-routes-mapper/badge.svg)](https://coveralls.io/github/aichbauer/express-routes-mapper)\n\n\u003e A simple package to map your routes for your expressjs application\n\n---\n**IMPORTANT: v1.0.2 fixed a security vulnerability. Every version up to v1.0.1 is not safe for production. Update your current version to v1.0.2 or higher. You can find more information [here](https://github.com/aichbauer/express-routes-mapper/issues/15).**\n---\n\n## Getting started\n\n- [Install](#install)\n- [Use](#use)\n  - [Routes](#routes)\n  - [Controller](#controller)\n  - [Middlewares](#middlewares)\n  - [Express with mapped Routes](#express-with-mapped-routes)\n- [Supported Methods](#supported-methods)\n- [Dynamic Routes](#dynamic-routes)\n\n## Install\n\n```sh\n$ npm i -S express-routes-mapper\n```\n\nor\n\n```sh\n$ yarn add express-routes-mapper\n```\n\n## Use\n\nAfter the installation you can import the package to your express project.\n\n## Routes\n\nCreate your routes file:\n\n```js\nconst routes = {\n  'POST /user': 'UserController.create'\n};\n\nexport default routes; // module.exports = routes;\n```\n\nEvery post request to your server to route '/user' will call the function 'create' on the 'UserController'.\n\n## Controller\n\nCreate a file named UserController.js\n\n```js\n// es6 class syntax\nexport default class UserController {\n  create (req, res) {\n    res.send('created a User with es6 class syntax');\n  };\n};\n\n// object factory pattern\nconst UserController = () =\u003e {\n  const create = (req, res) =\u003e {\n    res.send('created a User with without es6 class syntax');\n  };\n\n  return {\n    create,\n  };\n};\n\nexport default UserController; // module.exports = UserController;\n```\n\n## Middlewares\n\nMiddlewares allow you perform any set of operation on a particular route. They are executed from **top-to-bottom**, as they are arranged in the `middlewares` array.\n\nTo proceed to the next middleware or the controller, never forget to call the `next()` function.\n\nFor more examples, See [Middleware Example](./examples/app/config/routes.js).\n\n### Grouped Routes Middlewares\n\nMiddlewares can be added to a general set of routes. Such middlewares would be executed before any of the controller methods are called.\n\n```Javascript\nconst groupedMiddleware1 = (req, res, next) =\u003e {\n  next();\n};\n\nconst groupedMiddleware2 = (req, res, next) =\u003e {\n  next();\n};\n\nconst router = mapRoutes(routes, 'test/fixtures/controllers/', [groupedMiddleware1, groupedMiddleware2]);\n\n```\n\n### Middlewares On Routes\n\nMiddlewares can also be added to just a single route path.\n\n```Javascript\nconst checkIfAutheticated = (req, res, next) =\u003e {\n  console.log('authenticated');\n  next();\n};\n\nconst verifyFacebookAuth = (req, res, next) =\u003e {\n  console.log('unverified');\n  return res\n    .status(400)\n    .json({status: false, message: 'Sorry, you aren\\'t authorized on facebook'});\n};\n\nconst routes = {\n  'GET /user:id': {\n    path: 'UserController.get',\n    middlewares: [\n         checkIfAutheticated,\n         verifyFacebookAuth,\n    ],\n  },\n  \n  'POST /user': 'UserController.create'\n};\n```\n\n## Express with mapped Routes\n\nI assume you have a folder structure like this, but it can be adapted to any folder structure.\n\n```sh\n.\n+-- src\n|   +-- config\n|   |   +-- routes.js\n|   |\n|   +-- controllers\n|   |   +-- UserController.js\n|   |\n|   +-- models\n|   |\n|   app.js\n|\npackage.json\n```\n\nYour app.js could look a bit like this:\n\nThe magic happens here:\n\n- `import routes from './config/routes';` the file where all the routes are mapped\n- `import mapRoutes from 'express-routes-mapper';` the package that makes the mapping possible\n- `const mappedRoutes = mapRoutes(routes, 'src/controllers/');` tell router to use your routes\n- `app.use('/', mappedRoutes);` tell express to use the mapped routes\n\n```js\nimport express from 'express'; // const express = require('express');\nimport http from 'http'; // const http = require('http');\n\nimport mapRoutes from 'express-routes-mapper'; // const mapRoutes = require('express-routes-mapper');\nimport routes from './config/routes'; // const routes = require('./config/routes');\n\nconst app = express();\nconst server = http.Server(app);\nconst port = 4444;\n// mapRoutes takes two arguments\n//    - 1. the routes\n//    - 2. the path to your controllers from process.cwd();\nconst mappedRoutes = mapRoutes(routes, 'src/controllers/');\n\napp.use('/', mappedRoutes);\n\nserver.listen(port, () =\u003e {\n  console.log('There we go ♕');\n  console.log(`Gladly listening on http://127.0.0.1:${port}`);\n});\n```\n\n## Supported methods\n\nAll routes supported by the express framework is natively supported by this library (e.g. `GET`, `PUT`, `POST`, `DELETE` etc.).\n\n```js\nconst routes = {\n  'GET /someroute' : 'SomeController.somefunction',\n  'POST /someroute' : 'SomeController.somefunction',\n  'PUT /someroute' : 'SomeController.somefunction',\n  'DELETE /someroute' : 'SomeController.somefunction',\n  // etc.\n};\n```\n\n## Dynamic Routes\n\n Simply use a colon `:` for defining dynamic routes.\n\n ```js\n const routes = {\n   'GET /someroute/:id' : 'SomeController.someFunction',\n };\n ```\n\nIf you make a get request to `http://localhost/someroute/1` the number `1` (:id) is now in the `SomeController` accessible.\n\n```js\n// object factory pattern\nconst SomeController = () =\u003e {\n  const someFunction = (req, res) =\u003e {\n    const id = req.params.id;\n\n    // do some fency stuff with the id\n  };\n\n  return {\n    someFunction,\n  };\n};\n\nexport default SomeController; // module.exports = SomeController;\n```\n\n## Contribution\n\n1. Fork it!\n2. Create your feature branch: `git checkout -b feature-name`\n3. Commit your changes: `git commit -am 'Some commit message'`\n4. Push to the branch: `git push origin feature-name`\n5. Submit a pull request 😉😉\n\n## License\n\nMIT © Lukas Aichbauer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faichbauer%2Fexpress-routes-mapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faichbauer%2Fexpress-routes-mapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faichbauer%2Fexpress-routes-mapper/lists"}