# dynamic-routes

> Dynamic routes for Express.JS

Latest version **0.2.1** (published 2016-04-29) · Apache v2.0 license · 0 weekly downloads

## Install

```sh
npm install dynamic-routes
pnpm add dynamic-routes
yarn add dynamic-routes
bun add dynamic-routes
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2016-04-29 |
| First published | 2013-09-05 |
| Weekly downloads | 0 |
| License | Apache v2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Ahmed |
| Maintainers | oneofone |
| Keywords | express, dynamic-routes, web, app |

## Links

- npm: https://www.npmjs.com/package/dynamic-routes
- Repository: https://github.com/OneOfOne/dynamic-routes
- Homepage: https://github.com/OneOfOne/dynamic-routes#readme
- Issues: https://github.com/OneOfOne/dynamic-routes/issues
- npm.io page: https://npm.io/package/dynamic-routes

## Dependencies (2)

- [express](https://npm.io/package/express.md) >= 3.x
- [body-parser](https://npm.io/package/body-parser.md) >= 1.2.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.2.1 (latest) — 2016-04-29
- 0.2.0 — 2016-04-29
- 0.0.3 — 2014-05-19
- 0.0.2 — 2013-09-05
- 0.0.1 — 2013-09-05

## README

# dynamic-routes

> A dynamic router / loader for http://expressjs.com/

## Install

``` bash
npm install dynamic-routes --save
```


## Usage

> **DynamicRoutes(app, path_to_routes, [options])**


## Examples :

#### Express.JS Server :

```js
var express = require('express'),
	http = require('http'),
	path = require('path'),
	DynamicRoutes = require('dynamic-routes'),
	app = express();

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.use(express.logger('dev'));
app.use(bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('secrets secrets, we got extra'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

if ('development' == app.get('env')) {
	app.use(express.errorHandler());
}

DynamicRoutes(app, __dirname + '/routes/');
// or

/*
DynamicRoutes(app, {
	src: __dirname + '/routes/',
	ignore: [],
	pattern: /\.js$/
});
*/

http.createServer(app).listen(app.get('port'), function(){
	console.log('Express server listening on port ' + app.get('port'));
});

```
#### Creating compatible routes :

##### Using an object :

```js
//routes/index.js
module.exports = {
	priority: 1000, //this is the `/` handler, therefore it should be the last route.
	path: '/',

	//this function gets passed the express object one time for any extra setup
	init: function(app) {
		
	},
    
    //a middleware that is called from any HTTP methods
    ALL: function(req, res, next) {
        res.send(' - Common Middleware -');
        next();
    }
    
    //HTTP method specific routes
	GET: function(req, res) {
		res.end('GET ');
	},
	
	POST: function(req, res) {
        res.json(req.body);
        res.end('POST ');
	},
    
    //routes supports GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, PATCH method(all uppercase)
};
```

##### Routing multiple middlewares by assigning an array of middlewares :

```js
module.exports = {
    priority: 1000,
    path: '/',
    
    init: function(app){
        
    }
    
    ALL: [
        function(req, res, next){
            res.send(' - Common Middleware 1 - ');
            next();
        },
        function(req, res, next){
            res.send(' - Common Middleware 2 - ');
            next();
        }
    ],
    
    GET: [
        function(req, res, next){
            res.send(' GET 1 ');
            next();
        },
        function(req, res){
            res.end('end of GET');
        }
    ],
    
    POST: [
        function(req, res){
            res.json(req.body);
            res.end('end of POST');
        }
    ]
    /* an array with single route, that is equivalent to
    
    POST: function(req, res){
        res.json(req.body);
        res.end('end of POST');
    }
    
    */
}

```

##### Using a function :

```js
var user = module.exports = function(app) {
	"use strict";
	//other setup
	app.get(function(req, res) {
		res.json({'user': [req.params.id, req.params, req.query, req.body]});
	});
};

user.priority = 1;
user.path = '/user/:id?';

```

##### This is equivalent to :


```js
module.exports = {
	path: '/user/:id?',
	priority: 1,
	init: function (app) {
		//setup db
	},
	
	GET: function(req, res) {
		res.json({'user': [req.params.id, req.params, req.query, req.body]});
	}
}
```

## Notes
- As of Express 4.x, it's recommended to use `require('body-parser')()` instead of `express.bodyParser()`, check [`this`](http://stackoverflow.com/questions/5710358/how-to-get-post-query-in-express-node-js/20132867#20132867).

---
_Source: https://npm.io/package/dynamic-routes · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
