# express-route

> express-route is a library that let you organize the routes for express applications.

Latest version **0.1.4** (published 2013-06-05) · MIT license · 0 weekly downloads

## Install

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

## 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.1.4 |
| Published | 2013-06-05 |
| First published | 2013-05-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | ~0.8.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Adam Babik |
| Maintainers | dreame4 |
| Keywords | route, router, express |

## Links

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

## 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.1.4 (latest) — 2013-06-05
- 0.1.3 — 2013-06-04
- 0.1.2 — 2013-06-01
- 0.1.1 — 2013-06-01
- 0.1.0 — 2013-05-28
- 0.0.1 — 2013-05-28

## README

# express-route

express-route is a library that let you organize the routes for the express applications.

## Features

* single method API to read and apply the routes
* ability to read routes synchronously or asynchronously
* versatile format of the route definition to support all cases

## Usage

Firstly, you need to define the routes. The route can be a function, an object or an array of objects. They can be in one file or in many files.

The simplest definition may look like this:

```javascript
module.exports = {
	'/home': function (req, res, next) {
		res.end('Hello!');
	}
};
```

This definition is simply translated to the following code:

```javascript
app.get('/', function (req, res, next) {
	res.end('Hello!');
});
```

`get` is a default HTTP method. You can change it using an object definition:

```javascript
module.exports = {
	'/home': {
		methods: ['post'],
		fn: function (req, res, next) {
			res.end('Hello!');
		}
	}
};
```

To apply the routes to the express application you need to call `route` with valid parameters:

```javascript
var route = require('express-route');

// app is a reference to the express application
// './routes' is a path to the directory with the routes
// the third parameter is a configuration object
route(app, './routes', {
	// retrieve routes synchronously
	sync: true,

	// action to invoke when route is marked as restricted: true
	ensureRestriction: function (req, res, next) {
		if (!user.authorized()) {
			res.status(403).end('Forbidden');
			return;
		}
		next();
	}
});
```

## Route definition examples

The definition of the route may be slightly more complicated:

```javascript
'use strict';

module.exports = {

	// route with restricted setting on

	'/user': {
		fn: function (req, res) {
			res.end('user');
		},
		restricted: true
	},

	// route that accepts several methods

	'/user/categories': {
		fn: function (req, res) {
			res.end('method ' + req.method + ' post #' + req.params.id);
		},
		methods: ['get', 'post']
	},

	// several actions with different HTTP methods for one route

	'/user/posts/:id': [
		{
			fn: function (req, res) {
				res.end('method ' + req.method + ' post #' + req.params.id);
			},
			restricted: true,
			methods: ['get', 'delete']
		},
		{
			fn: function (req, res) {
				res.end('method ' + req.method + ' post #' + req.params.id);
			},
			restricted: true,
			methods: ['post']
		}
	]
};
```

## API

#### **`route(app: Object, path: String, settings: Object)`**

Apply routes to the express application.

* `app` - reference to the express application,
* `path` - path to the folder or the file with the routes,
* `settings` - a configuration object,
	* `sync: Boolean` - whether the load of the routings is synchronous or not (optional, `true` by default),
	* `ensureRestriction: Function` - middleware which check if the request is authenticated (optional, by default it's a function that accepts all requests),
	* `ext: Array` - array with valid file extensions, e.g. `['.route']` (optional, `['.js']` by default).

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