Socket
Socket
Sign inDemoInstall

express-routes-mapper

Package Overview
Dependencies
167
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.1 to 1.0.0

88

lib/index.js

@@ -1,29 +0,49 @@

/* eslint global-require: 0 */
/* eslint import/no-dynamic-require: 0 */
import express from 'express';
import entries from 'object.entries';
'use strict';
const router = express.Router();
var _object = require('object.entries');
function mapRoutes(routes, pathToController = '../../../app/controllers/') {
let requestMethodPath;
var _object2 = _interopRequireDefault(_object);
let requestMethod;
let path;
var _express = require('express');
let controllerMethod;
let controller;
var _express2 = _interopRequireDefault(_express);
let handler;
let Handler;
let c;
var _path = require('path');
const routesArr = entries(routes);
var _path2 = _interopRequireDefault(_path);
routesArr.forEach((value) => {
requestMethodPath = value[0].replace(/\s\s+/g, ' ');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
requestMethod = (requestMethodPath.split(' ')[0]).toLocaleLowerCase();
path = requestMethodPath.split(' ')[1];
var router = _express2.default.Router();
var cwd = process.cwd();
var isConstructor = function isConstructor(func) {
try {
new func();
} catch (err) {
return false;
}
return true;
};
var mapRoutes = function mapRoutes(routes, pathToController) {
var requestMethodPath = void 0;
var requestMethod = void 0;
var controllerMethod = void 0;
var controller = void 0;
var contr = void 0;
var handler = void 0;
var myPath = void 0;
var myPathToController = _path2.default.join(cwd, pathToController);
var routesArr = (0, _object2.default)(routes);
routesArr.forEach(function (value) {
requestMethodPath = value[0].replace(/\s\s+/g, ' ');
requestMethod = requestMethodPath.split(' ')[0].toLocaleLowerCase();
myPath = requestMethodPath.split(' ')[1];
controller = value[1].split('.')[0];

@@ -33,23 +53,31 @@ controllerMethod = value[1].split('.')[1];

try {
handler = require('' + myPathToController + controller);
var isConstructable = isConstructor(handler);
if (isConstructable) {
contr = new handler();
} else {
contr = handler();
}
} catch (err) {
require('babel-register');
Handler = require(pathToController + controller).default;
c = new Handler();
} catch (err) {
handler = require(pathToController + controller);
c = handler;
handler = require('' + myPathToController + controller).default;
contr = new handler();
}
if (requestMethod === 'get') {
router.route(path).get(c[controllerMethod]);
router.route(myPath).get(contr[controllerMethod]);
} else if (requestMethod === 'post') {
router.route(path).post(c[controllerMethod]);
router.route(myPath).post(contr[controllerMethod]);
} else if (requestMethod === 'put') {
router.route(path).put(c[controllerMethod]);
router.route(myPath).put(contr[controllerMethod]);
} else if (requestMethod === 'delete') {
router.route(path).delete(c[controllerMethod]);
router.route(myPath).delete(contr[controllerMethod]);
}
});
return router;
}
};
module.exports = mapRoutes;
module.exports = mapRoutes;
{
"name": "express-routes-mapper",
"version": "0.1.1",
"version": "1.0.0",
"description": "a small mapper for express routes",
"main": "./dist/index.js",
"main": "lib/index.js",
"scripts": {
"start": "gulp",
"lint": "eslint ./lib/index.js",
"test": "gulp && nyc ava ./tests/lib/*",
"prepublish": "npm start",
"pretest": "npm run lint && npm run babel",
"test": "nyc ava",
"lint": "eslint ./src/index.js",
"lintFix": "eslint ./src/index.js --fix",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"prepublish": "npm run babel",
"precommit": "npm run lint",
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
"prepush": "npm test",
"babel": "babel src --out-dir lib"
},

@@ -28,2 +31,5 @@ "keywords": [

"dependencies": {
"babel-core": "^6.23.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.2.1",
"babel-register": "^6.23.0",

@@ -35,5 +41,3 @@ "express": "^4.15.2",

"ava": "^0.18.2",
"babel-core": "^6.23.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.2.1",
"babel-cli": "^6.24.0",
"coveralls": "^2.12.0",

@@ -43,4 +47,2 @@ "eslint": "^3.17.1",

"eslint-plugin-import": "^2.2.0",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"husky": "^0.13.2",

@@ -56,3 +58,3 @@ "nyc": "^10.1.2"

"exclude": [
"tests",
"test",
"examples"

@@ -59,0 +61,0 @@ ]

@@ -5,42 +5,40 @@ # express-routes-mapper

> a simple package to map your routes for your expressjs application
> A simple package to map your routes for your expressjs application
## getting started
## Getting started
- [start from ground](#start-from-ground)
- [supported methods](#supported-methods)
- [dynamic routes](#dynamic-routes)
- [set path to controller](#set-path-to-controller)
- [Install](#install)
- [Use](#use)
- [Routes](#routes)
- [Controller](#controller)
- [Express with mapped Routes](#express-with-mapped-routes)
- [Supported Methods](#supported-methods)
- [Dynamic Routes](#dynamic-routes)
## start from ground
## Install
This is a example for a simple rest API.
```sh
$ npm i -S express-routes-mapper
```
### 1.) npm install
or
```sh
$ npm i -S express-routes-mapper
$ yarn add express-routes-mapper
```
### 2.) mapped routes
## Use
After the installation you can import the package to your express project.
### Routes
Create your routes file:
```js
// es6
const routes = {
'POST /user': 'UserController.create'
};
}
export default routes;
//es5
module.exports = {
'POST /user': 'UserController.create'
}
export default routes; // module.exports = routes;
```

@@ -50,3 +48,3 @@

### 3.) the controller
### Controller

@@ -56,33 +54,29 @@ Create a file named UserController.js

```js
//es6
// es6 class syntax
export default class UserController {
create (req, res) {
res.send('created a User with es6 class syntax');
};
};
res.send('created a User with es6');
// object factory pattern
const UserController = () => {
const create = (req, res) => {
res.send('created a User with without es6 class syntax');
};
}
return {
create,
};
};
}
//es5
module.exports = {
'create': function (req, res) {
res.send('created a User with es5');
}
}
export default UserController; // module.exports = UserController;
```
### 4.) tell express.js app to use our routes
### Express with mapped Routes
I assume you have a folder structure like this, but it can be adapted to any folder structure.
I assume you have a folder structure like this, but it can be adapted to any folder structure.
If you have a different, folder structure, and want to link to a different path look [here](#set-path-to-controller).
```
```sh
.

@@ -106,47 +100,29 @@ +-- src

The magic happens here:
* `import routes from './config/routes';` the file where all the routes are mapped
* `import mapRoutes from 'express-routes-mapper';` the package that makes the mapping possible
* `app.use('/', mapRoutes(routes));` tell express to use the mapped routes
and here
* `var routes = require('./config/routes');` the file where all the routes are mapped
* `var mapRoutes = require('express-routes-mapper');` the package that makes the mapping possible
* `app.use('/', mapRoutes(routes));` tell express to use the mapped routes
- `import routes from './config/routes';` the file where all the routes are mapped
- `import mapRoutes from 'express-routes-mapper';` the package that makes the mapping possible
- `const mappedRoutes = mapRoutes(routes, 'src/controllers/');` tell router to use your routes
- `app.use('/', mappedRoutes);` tell express to use the mapped routes
```js
//es6
import express from 'express';
import http from 'http';
import express from 'express'; // const express = require('express');
import http from 'http'; // const http = require('http');
import routes from './config/routes';
import mapRoutes from 'express-routes-mapper';
import mapRoutes from 'express-routes-mapper'; // const mapRoutes = require('express-routes-mapper');
import routes from './config/routes'; // const routes = require('./config/routes');
const app = express();
const server = http.Server(app);
const port = 3338;
const port = 4444;
// mapRoutes takes two arguments
// - 1. the routes
// - 2. the path to your controllers from process.cwd();
const mappedRoutes = mapRoutes(routes, 'src/controllers/');
app.use('/', mapRoutes(routes));
app.use('/', mappedRoutes);
server.listen(port, function() {
server.listen(port, () => {
console.log('There we go ♕');
console.log(`Gladly listening on http://127.0.0.1:${port}`);
});
//es5
var express = require('express');
var http = require('http');
var routes = require('./config/routes');
var mapRoutes = require('express-routes-mapper');
var app = express();
var server = http.Server(app);
var port = 3339;
app.use('/', mapRoutes(routes));
server.listen(port, function(){
console.log('There we go ♕');
console.log('Gladly listening on http://127.0.0.1:' + port);
});
```

@@ -156,96 +132,43 @@

* GET
* POST
* PUT
* DELETE
- **GET**
- **POST**
- **PUT**
- **DELETE**
```js
{
'GET /someroute' : 'SomeController.somefunction',
'POST /someroute' : 'SomeController.somefunction',
'PUT /someroute' : 'SomeController.somefunction',
'DELETE /someroute' : 'SomeController.somefunction'
}
const routes = {
'GET /someroute' : 'SomeController.somefunction',
'POST /someroute' : 'SomeController.somefunction',
'PUT /someroute' : 'SomeController.somefunction',
'DELETE /someroute' : 'SomeController.somefunction',
};
```
## Dynamic routes
## Dynamic Routes
Simply use a colon ':' for defining dynamic routes.
Simply use a colon `:` for defining dynamic routes.
```js
{
'GET /someroute/:id' : 'SomeController.somefunction'
}
const routes = {
'GET /someroute/:id' : 'SomeController.someFunction',
};
```
If you make a get request to `http://localhost/someroute/1` the 1 (:id) is now in the 'SomeController accessible.
If you make a get request to `http://localhost/someroute/1` the number `1` (:id) is now in the `SomeController` accessible.
```js
//es6
export default class SomeController {
// object factory pattern
const SomeController = () => {
const someFunction = (req, res) => {
const id = req.params.id;
somefunction (req, res) {
// do some fency stuff with the id
};
let id = req.params.id;
return {
someFunction,
};
};
}
}
//es5
module.exports = {
'somefunction': function (req, res) {
var id = req.params.id
}
}
export default SomeController; // module.exports = SomeController;
```
## set path to controller
The only differnce is that you pass in the path to your file in the mapRoutes function.
* app.use('/', mapRoutes(routes, '../../../path/to/new/file/'));
```js
//es6
import express from 'express';
import http from 'http';
import routes from './config/routes';
import mapRoutes from 'express-routes-mapper';
const app = express();
const server = http.Server(app);
const port = 3338;
app.use('/', mapRoutes(routes, '../../../path/to/new/file/'));
server.listen(port, function() {
console.log('There we go ♕');
console.log(`Gladly listening on http://127.0.0.1:${port}`);
});
//es5
var express = require('express');
var http = require('http');
var routes = require('./config/routes');
var mapRoutes = require('express-routes-mapper');
var app = express();
var server = http.Server(app);
var port = 3339;
app.use('/', mapRoutes(routes, '../../../path/to/new/file/'));
server.listen(port, function(){
console.log('There we go ♕');
console.log('Gladly listening on http://127.0.0.1:' + port);
});
```

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc