# route-dir

> Recursively map directory structure to URIs

Latest version **0.3.1** (published 2015-04-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install route-dir
pnpm add route-dir
yarn add route-dir
bun add route-dir
```

## 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.3.1 |
| Published | 2015-04-17 |
| First published | 2014-05-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Oleg Sklyar |
| Maintainers | osklyar |
| Keywords | routing, DSL, express, dispatch, directory routing |

## Links

- npm: https://www.npmjs.com/package/route-dir
- Repository: https://github.com/qucado/route-dir
- Issues: https://github.com/qucado/route-dir/issues
- npm.io page: https://npm.io/package/route-dir

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.3.1 (latest) — 2015-04-17
- 0.3.0 — 2015-01-06
- 0.2.0 — 2014-05-05

## README

# Recursively map directory structure to URIs

`route-dir` provides functionality to recursively map a directory structure to HTTP server URI supporting basic DSL syntax (in form of `:id`) in path names. The module was designed and tested with `express`, but does not have it as a dependency and will work in the same manner with other frameworks that use `app.get`, `app.post` etc. to register dispatch patterns. The module does not require that all routing patterns are registered in this manner and can be used in addition to any routing defined by other means.

## Installation

    $ npm install route-dir
	
Tests:

    $ npm test

## Usage

The code to handle requests needs to be arranged in a directory structure (e.g. `server/routes`) following these rules:

* `server/routes/index.js` will map to `/`
* `server/routes/path/user/index.js` will map to `/path/user/` 
* `server/routes/path/user.js` will map to `/path/user` (different from above)
* `server/routes/_colon_lang/user/_colon_id` will map to `/:lang/user/:id`
* `server/routes/_star_` will map to `/*` as the last entry in the rule set

Request handlers are defined as functions `get`, `post` etc. exported by a module under `server/routes`:

```javascript
exports.get = function(conn, config) {
    return function(req, res) {
        res.render("user/signup");
    };
};

exports.post = function(conn, config) {
    // 'conn' and 'server' can be used here e.g. to load a data model 
    // given a specific connection and using specific configuration
    return function(req, res, next) {
        var User = require("usermodel").getModel(conn);
        User.registerNewUser(req.body, function(err, user) {
            if (err) {
                next(err);
            } else {
                res.send(200, { userId: user.id });
                res.end();
            }
        });
    };
};
```

Here `conn` and `config` are optional arguments intended to pass a specific DB-connection (e.g. `mongoose` connection for connection-specific models as in the above example) and server configuration that may be needed in handling the request. Each exported function is expected to return a standard handler taking `req, res` or `req, res, next` arguments. These will be directly registered with the server: 

```javascript
...
var routing = require("route-dir");
var server = express();
...
server.use(server.router);
// register routes without passing connection or config to the handlers
routing.route(server, "server/routes");
// register routes passing in a specific connection and a config
// ensure the locations are not clashing with the above...
routing.route(server, "server/more-routes", mongoose.createConnection(config.db), config);
```

Using the module as an express middleware is currently under development and will be added shortly:

```javascript
server.use(routing.route("server/routes"));
```


## License

    The MIT License

    Copyright (c) 2014 Oleg Sklyar <osklyar@qucado.com>

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    'Software'), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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