# express-middleware-file-routes

> A lightning fast middleware for express to transform a folder structure into an API

Latest version **0.3.1** (published 2017-11-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install express-middleware-file-routes
pnpm add express-middleware-file-routes
yarn add express-middleware-file-routes
bun add express-middleware-file-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.3.1 |
| Published | 2017-11-29 |
| First published | 2017-11-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | woobione |
| Keywords | express, middleware, file, load, require, route, router |

## Links

- npm: https://www.npmjs.com/package/express-middleware-file-routes
- Repository: https://github.com/woobione/express-middleware-file-routes
- Homepage: https://github.com/woobione/express-middleware-file-routes#readme
- Issues: https://github.com/woobione/express-middleware-file-routes/issues
- npm.io page: https://npm.io/package/express-middleware-file-routes

## Dependencies (1)

- [express](https://npm.io/package/express.md) latest

## 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) — 2017-11-29
- 0.3.0 — 2017-11-29
- 0.2.1 — 2017-11-08
- 0.2.0 — 2017-11-08
- 0.1.1 — 2017-11-07
- 0.1.0 — 2017-11-07
- 0.0.5 — 2017-11-06
- 0.0.4 — 2017-11-05
- 0.0.3 — 2017-11-05
- 0.0.2 — 2017-11-05
- 0.0.1 — 2017-11-04

## README

# express-middleware-file-routes
A lightning fast middleware for [express](https://www.npmjs.com/package/express) to transform a folder structure into an API.

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Github Issues][issues-image]][issues-url]
[![License][license-image]][license-url]

## How does it work?
By looking in a folder (default: **/routes**) - API routes are created based on the file structure.

A folder or a file would create the same structure like so:

```/home.js -> /home```
and
```/home/index.js -> /home```

<sub>*(what type of files that are allowed to created controllers can be configured by the **fileTypes** option)*</sub>


## Get started
### Installation
```
npm install express-middleware-file-routes
```

### Basic Setup
```javascript
const express = require('express');
const fileBasedApi = require('express-middleware-file-routes');

// load express
const app = express();

// this would point the root of your server to the api middleware
// accessible via /{your}/{path}
app.use(fileBasedAPI());

// it can also be mounted under a sub path like so:
// accessible via /api/{your}/{path}
app.use('/api', fileBasedAPI());

// boot your app on locahost:3000
app.listen(3000);
```

### Defining your first controller
Inside your **/routes** folder - create a file called **index.js**
```javascript
module.exports = {

    get: (req, res) => {
        res.json('You succeeded!');
    },

    post: (req, res) => {
        res.json('A post has been made, your highness');
    }

};
```
You should now be able to navigate to **localhost:3000** in your browser and see the *"You succeeded!"* message.

Likewise, a post to the same route should give you the *"A post has been made, your highness"* message.

### Advanced usage
Since the express router is very powerful, you could utilize that power for some fun magic.

For example - putting a file (or folder) name within parentheses **/home/(whatHome).js** would create a dynamic parameter just like express would with the **/home/:whatHome** syntax.

Suppose this being your **(whatHome).js** file:
```javascript
module.exports = {

    get: (req, res) => {
        res.json(req.params);
    }

};
```
Accessing **localhost:3000/home/my-awesome-home** would go through the above controller and return this message:
```json
{
    "whatHome": "my-awesome-home"
}
```

## Options
Options can be passed into the constructor like so:
```javascript
app.use(fileBasedAPI({
    optionName: 'value'
}));
```

### beforeSetupRoute
*(this hook will run before mounting each and every route found)*

**optionName**: *beforeSetupRoute*

**type**: ```callable(route, controller)```

**defaultValue**: ```(void)```


### fileTypes
*(what type of files should be loaded as controllers)*

**optionName**: *fileTypes*

**type**: ```array[string]```

**defaultValue**: ```['js']```


### rootFile
*(name of the file that defines the root state **/** of a folder - defaults to **index**(.js))*

**optionName**: *rootFile*

**type**: ```string```

**defaultValue**: ```'index'```


### routes
*(path to the directory where routes are located)*

**optionName**: *routes*

**type**: ```string```

**defaultValue**: ```'/routes'```


### unsupportedMethodHandler
*(fallback handler when no specific handler **POST|GET|..** is found on a route)*

**optionName**: *routes*

**type**: ```callable(req, res)```

**defaultValue**: ```Returns status code 405 (method not supported)```


## License
Copyright (c) 2017 Anton Netterwall

Available under the MIT license. See the LICENSE file for more info.


[downloads-image]: https://img.shields.io/npm/dt/express-middleware-file-routes.svg
[downloads-url]: https://npmjs.org/package/express-middleware-file-routes
[issues-image]: https://img.shields.io/github/issues-raw/woobione/express-middleware-file-routes.svg
[issues-url]: https://github.com/woobione/express-middleware-file-routes
[license-image]: https://img.shields.io/github/license/woobione/express-middleware-file-routes.svg
[license-url]: http://opensource.org/licenses/ISC
[npm-image]: https://img.shields.io/npm/v/express-middleware-file-routes.svg
[npm-url]: https://npmjs.org/package/express-middleware-file-routes

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