# folder-router

> A simple router based on folder name

Latest version **1.0.4** (published 2016-05-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install folder-router
pnpm add folder-router
yarn add folder-router
bun add folder-router
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2016-05-16 |
| First published | 2016-05-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | dorianamouroux |
| Maintainers | dorianamouroux |
| Keywords | router, express, simple, folder, name, based, api, rest |

## Links

- npm: https://www.npmjs.com/package/folder-router
- Repository: https://github.com/dorianamouroux/folder-router
- Homepage: https://github.com/dorianamouroux/folder-router#readme
- Issues: https://github.com/dorianamouroux/folder-router/issues
- npm.io page: https://npm.io/package/folder-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

- 1.0.4 (latest) — 2016-05-16
- 1.0.3 — 2016-05-11
- 1.0.2 — 2016-05-11
- 1.0.1 — 2016-05-11
- 1.0.0 — 2016-05-11

## README

# folder-router

## Introduction
Folder-router is a tiny express router to get started with a new NodeJS project. You can put all your route files in a single directory, folder-router will be able to open and inject all your endpoint in your express instance. You can also put your file in sub directory and access it through sub-url.

## Installation

You need NPM to install folder-router in your NodeJS project.

```sh
$ npm install --save folder-router
```

You can now use it in your main server file.  
The first parameters must be your express instance, the second one must be the location of your routes directory. I highly recommand to use absolute location.
```js
var app = require('express')();
var router = require('folder-router');

router(app, __dirname + '/routes');

app.listen(3000);
```

## Creating your routes

In your routes directory, you can create as much as file as you want, everything will be read and add to express by folder-router. A route file MUST export a express.Router() instance as below  :
  
routes/book.js
```js
var Router = require('express').Router();

Router.get('/book', function(req, res) {
	res.end('All Books !');
});

Router.get('/book/:id', function(req, res) {
	res.end('Books with id = ', req.params.id);
});

Router.post('/book', function(req, res) {
	res.end('Adding a new book')
});

module.exports = Router;
```
Here is the list of all end points :
- GET http://localhost:3000/book
- GET http://localhost:3000/book/5
- POST http://localhost:3000/book
  
-------------
Also, you can create a directory "book" and write your routes like so :  
routes/book/book.js
```js
var Router = require('express').Router();

Router.get('/', function(req, res) {
	res.end('All Books !');
});

Router.get('/:id', function(req, res) {
	res.end('Books with id = ', req.params.id);
});

Router.post('/', function(req, res) {
	res.end('Adding a new book')
});

module.exports = Router;
```
The name of the file has no impact. The end point remains the same because it takes the name of the sub directory

---------

One other solution for that, you can exports the base url with the root variable

routes/book.js
```js
var Router = require('express').Router();

Router.get('/', function(req, res) {
	res.end('All Books !');
});

Router.get('/:id', function(req, res) {
	res.end('Books with id = ', req.params.id);
});

Router.post('/', function(req, res) {
	res.end('Adding a new book')
});

module.exports = Router;
module.exports.root = '/book'; // here
```


## Tests

```
$ npm test
```
or 
```
$ mocha ./test
```

## Contribute
All ways to contribute to folder-router are highly appreciated:  
- improving the readme  
- writing features  
- writing tests  
- writing a tutorial  
- troubleshooting reported issues
  
Thank you very much !

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