https://github.com/davidcornu/path-finder
Named routes for Express.
https://github.com/davidcornu/path-finder
Last synced: 8 months ago
JSON representation
Named routes for Express.
- Host: GitHub
- URL: https://github.com/davidcornu/path-finder
- Owner: davidcornu
- Created: 2012-11-24T08:18:42.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2013-01-31T04:58:30.000Z (over 13 years ago)
- Last Synced: 2024-04-15T10:44:22.944Z (about 2 years ago)
- Language: JavaScript
- Size: 145 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Pathfinder
Simple named routes for [Express](https://github.com/visionmedia/express).
[](https://travis-ci.org/davidcornu/path-finder)
## Deprecation Warning
The following methods will print deprecation messages when used as extending the
express `app` variable is error prone and doesn't really provide a better interface,
especially when calling routes from controllers (`req.app.p('login')` is a lot
more verbose than simply requiring `path-finder`).
```javascript
app.p(pathname)
app.addPath(pathname, path)
```
## Usage
1. Install via [npm](https://npmjs.org)
```
$ npm install path-finder
```
2. Extend your Express `app`
```javascript
var express = require('express');
var app = express();
var pathFinder = require('path-finder');
pathFinder.extend(app);
```
3. Profit
```javascript
// Standard Express routing does not change
app.get('/users', function(){...})
// Passing in a name stores the path
app.get('/home', 'home', function(){...})
app.post('/user/:id', 'user', function(){...});
// Paths can be accessed via
pathFinder.path('home'); //-> '/home'
pathFinder.path('user', {id: 10}); //-> '/user/10'
// Passing in additional options adds them to the query string
pathFinder.path('home', {p: 1}); //-> '/home?p=1'
// Ad-hoc paths can also be defined
pathFinder.addPath('promotions', '/promotions');
pathFinder.path('promotions'); //-> '/promotions'
```
A `p` method is also made available to views
```
a(href=p('promotions'))
| View our promotions
```
## Development
Clone the repo
```
$ git clone git@github.com:davidcornu/path-finder.git
```
Install dependencies
```
$ npm install
```
Run the tests
```
$ npm test
```