express-routes-mapper
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -0,1 +1,8 @@ | ||
1.1.0 - November, 23 2018 | ||
* d844888 Chore: add license (#34) (Lukas Aichbauer) | ||
* a8afe1f Chore: rm package-lock use yarn.lock (#33) (Lukas Aichbauer) | ||
* fa631f6 Feat(Middleware): add Middleware Functionality (#30) (Tochukwu Nkemdilim) | ||
* 2a61ffd Docs: fix typo in important message (#26) (Julio Ojeda) | ||
1.0.6 - October, 09 2018 | ||
@@ -2,0 +9,0 @@ |
@@ -12,2 +12,4 @@ "use strict"; | ||
require("core-js/modules/es6.array.filter"); | ||
require("core-js/modules/es6.regexp.split"); | ||
@@ -17,2 +19,6 @@ | ||
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); | ||
require("core-js/modules/es6.array.is-array"); | ||
require("core-js/modules/web.dom.iterable"); | ||
@@ -24,2 +30,4 @@ | ||
var _util = require("util"); | ||
var _express = _interopRequireDefault(require("express")); | ||
@@ -36,2 +44,4 @@ | ||
var mapRoutes = function mapRoutes(routes, pathToController) { | ||
var middlewareGenerals = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var router = _express.default.Router(); | ||
@@ -51,8 +61,40 @@ | ||
routesArr.forEach(function (value) { | ||
var middlewares; // to let use an array or only one function as general middlewares | ||
if (Array.isArray(middlewareGenerals)) { | ||
middlewares = (0, _toConsumableArray2.default)(middlewareGenerals); | ||
} else if (typeof middlewareGenerals === 'function') { | ||
middlewares = [middlewareGenerals]; | ||
} else { | ||
middlewares = []; | ||
} | ||
requestMethodPath = value[0].replace(/\s\s+/g, ' '); | ||
requestMethod = requestMethodPath.split(' ')[0].toLocaleLowerCase(); | ||
myPath = requestMethodPath.split(' ')[1]; | ||
controller = (0, _splitByLastDot.default)(value[1])[0]; | ||
controllerMethod = (0, _splitByLastDot.default)(value[1])[1]; | ||
if ((0, _util.isString)(value[1])) { | ||
controller = (0, _splitByLastDot.default)(value[1])[0]; | ||
controllerMethod = (0, _splitByLastDot.default)(value[1])[1]; | ||
} else { | ||
// contains middlewares and other configuration | ||
var props = value[1]; // Extract controller paths | ||
if (props.path !== undefined) { | ||
controller = (0, _splitByLastDot.default)(props.path)[0]; | ||
controllerMethod = (0, _splitByLastDot.default)(props.path)[1]; | ||
} // Extract middlewares. | ||
if (props.middlewares !== undefined && Array.isArray(props.middlewares)) { | ||
var _middlewares; | ||
(_middlewares = middlewares).push.apply(_middlewares, (0, _toConsumableArray2.default)(props.middlewares)); | ||
} | ||
} | ||
middlewares = middlewares.filter(function (el) { | ||
return el != null; | ||
}); | ||
try { | ||
@@ -74,3 +116,3 @@ handler = require("".concat(myPathToController).concat(controller)); | ||
router.route(myPath)[requestMethod](contr[controllerMethod]); | ||
router.route(myPath)[requestMethod](middlewares, contr[controllerMethod]); | ||
}); | ||
@@ -77,0 +119,0 @@ return router; |
{ | ||
"name": "express-routes-mapper", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "a small mapper for express routes", | ||
@@ -13,2 +13,3 @@ "main": "lib/index.js", | ||
"prepublish": "npm run babel", | ||
"build": "npm run babel", | ||
"precommit": "npm run lint", | ||
@@ -15,0 +16,0 @@ "prepush": "npm test", |
@@ -8,3 +8,3 @@ # express-routes-mapper | ||
--- | ||
**IMPORTANT: v1.0.2 fixed a security vulnerability. Every version up to v1.0.1 is not save for production. Update your current version to v1.0.2. You can find more information [here](https://github.com/aichbauer/express-routes-mapper/issues/15).** | ||
**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).** | ||
--- | ||
@@ -18,2 +18,3 @@ | ||
- [Controller](#controller) | ||
- [Middlewares](#middlewares) | ||
- [Express with mapped Routes](#express-with-mapped-routes) | ||
@@ -39,3 +40,3 @@ - [Supported Methods](#supported-methods) | ||
### Routes | ||
## Routes | ||
@@ -54,3 +55,3 @@ Create your routes file: | ||
### Controller | ||
## Controller | ||
@@ -81,5 +82,59 @@ Create a file named UserController.js | ||
## Middlewares | ||
### Express with mapped Routes | ||
Middlewares 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. | ||
To proceed to the next middleware or the controller, never forget to call the `next()` function. | ||
For more examples, See [Middleware Example](./examples/app/config/routes.js). | ||
### Grouped Routes Middlewares | ||
Middlewares can be added to a general set of routes. Such middlewares would be executed before any of the controller methods are called. | ||
```Javascript | ||
const groupedMiddleware1 = (req, res, next) => { | ||
next(); | ||
}; | ||
const groupedMiddleware2 = (req, res, next) => { | ||
next(); | ||
}; | ||
const router = mapRoutes(routes, 'test/fixtures/controllers/', [groupedMiddleware1, groupedMiddleware2]); | ||
``` | ||
### Middlewares On Routes | ||
Middlewares can also be added to just a single route path. | ||
```Javascript | ||
const checkIfAutheticated = (req, res, next) => { | ||
console.log('authenticated'); | ||
next(); | ||
}; | ||
const verifyFacebookAuth = (req, res, next) => { | ||
console.log('unverified'); | ||
return res | ||
.status(400) | ||
.json({status: false, message: 'Sorry, you aren\'t authorized on facebook'}); | ||
}; | ||
const routes = { | ||
'GET /user:id': { | ||
path: 'UserController.get', | ||
middlewares: [ | ||
checkIfAutheticated, | ||
verifyFacebookAuth, | ||
], | ||
}, | ||
'POST /user': 'UserController.create' | ||
}; | ||
``` | ||
## Express with mapped Routes | ||
I assume you have a folder structure like this, but it can be adapted to any folder structure. | ||
@@ -137,6 +192,3 @@ | ||
- **GET** | ||
- **POST** | ||
- **PUT** | ||
- **DELETE** | ||
All routes supported by the express framework is natively supported by this library (e.g. `GET`, `PUT`, `POST`, `DELETE` etc.). | ||
@@ -149,2 +201,3 @@ ```js | ||
'DELETE /someroute' : 'SomeController.somefunction', | ||
// etc. | ||
}; | ||
@@ -181,1 +234,13 @@ ``` | ||
``` | ||
## Contribution | ||
1. Fork it! | ||
2. Create your feature branch: `git checkout -b feature-name` | ||
3. Commit your changes: `git commit -am 'Some commit message'` | ||
4. Push to the branch: `git push origin feature-name` | ||
5. Submit a pull request 😉😉 | ||
## License | ||
MIT © Lukas Aichbauer |
18996
7
111
239