# express-simplefile-router

> Transparent and simple file-based router for Express

Latest version **0.0.1** (published 2020-11-12) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install express-simplefile-router
pnpm add express-simplefile-router
yarn add express-simplefile-router
bun add express-simplefile-router
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2020-11-12 |
| First published | 2020-11-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Egor Egorov |
| Maintainers | egorfine |
| Keywords | express, router, route, middleware, connect |

## Links

- npm: https://www.npmjs.com/package/express-simplefile-router
- Repository: https://github.com/egorFiNE/express-simplefile-router
- Homepage: https://github.com/egorFiNE/express-simplefile-router#readme
- Issues: https://github.com/egorFiNE/express-simplefile-router/issues
- npm.io page: https://npm.io/package/express-simplefile-router

## 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.0.1 (latest) — 2020-11-12

## README

# express-simplefile-router

Transparent and simple filesystem-based router for Express.

Think how easy it is to create new routes in PHP and how obvious is the list of routes for a typical project? This is what I wanted in Express.

# Synopsis

0. `npm install express-simplefile-router`

1. Create `api-handlers/user/hello.get.js` in your project:

```javascript
module.exports = (req, res) => {
  res.send("Hello world");
};
```

2. Mount routes in your `server.js`:

```javascript
const expressSimpleFileRouter = require('express-simplefile-router');

const app = express();

// ...

app.use(
  '/api/',
  expressSimpleFileRouter({
    directory: 'api-handlers'
  })
);

const server = app.listen(1111);
```

3. Now you have a `GET` method handler on `/api/user/hello/`:

```bash
user:~$ curl http://localhost:1111/api/user/hello/
Hello world
```

# Documentation

## `expressSimpleFileRouter()`

Basically `expressSimpleFileRouter()` traverses a directory of files and mounts them as routes on an instance of [Express.router()](https://expressjs.com/en/4x/api.html#router). Simple as that.

It accepts two deconstructed arguments:

* `directory` (required) which directory to travel looking for files ending in `.post.js`, `.get.js`, `.delete.js`, `.put.js` or `.patch.js` and mounting them as routes;
* `prefix` (default `'/'`) string to prepend to all route paths when mounting on `Express.router()`;

## Route handler files

Every route handler file is a node module. Whatever you `module.exports` is supplied as second argument to `Express.router()` mount method (`.post()`, `.get()`, etc). So you can export either a standard handler `(req, res) => {...`, or an array of such. See `test/one/two.get.js` for the latter example.

Route handler file names are mapped to their route paths. I.e. `api/auth/user.post.js` becomes route `/api/user/` that handles `POST` methods.

# Development and maintenance

Tests: `npm run test` or `mocha test.js`.

Liner: `npm run lint` or `eslint index.js`.

# TODO

- [ ] Imagine a simple way to handle REST arguments (`/user/123`)
- [ ] Write jsdoc for `index.js`
- [ ] Collect developers' feedback
- [ ] Publish JSONHandler and ValidateSchema and add write docs for them all

# Footer

Author: Egor Egorov me@egorfine.com.<br>
License: MIT

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