Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wesleytodd/nighthawk
A wrapper around Express' router for the browser
https://github.com/wesleytodd/nighthawk
Last synced: about 1 month ago
JSON representation
A wrapper around Express' router for the browser
- Host: GitHub
- URL: https://github.com/wesleytodd/nighthawk
- Owner: wesleytodd
- License: isc
- Created: 2015-02-03T02:16:45.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2019-08-18T00:49:54.000Z (over 5 years ago)
- Last Synced: 2024-10-25T19:09:28.184Z (about 2 months ago)
- Language: JavaScript
- Size: 88.9 KB
- Stars: 55
- Watchers: 5
- Forks: 7
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Express' Router for the Browser
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Build Status](https://travis-ci.org/wesleytodd/nighthawk.svg?branch=master)](https://travis-ci.org/wesleytodd/nighthawk)
[![js-happiness-style](https://img.shields.io/badge/code%20style-happiness-brightgreen.svg)](https://github.com/JedWatson/happiness)
[![Sauce Test Status](https://saucelabs.com/buildstatus/wesleytodd123)](https://saucelabs.com/u/wesleytodd123)The Express Router is great! It is reliable and really simple. With Express 5.0 the router module was pulled out into a stand-alone package,
so I figured, why not see if it runs in the browser. Sure enough, it did! So this is a wrapper around the [Express Router](https://github.com/pillarjs/router)
package that layers on browser push state based location updates. I was heavily influenced by [Page.js](https://visionmedia.github.io/page.js/) and use a
very similar method to transparently catch page changes events.**NOTE**: Requires HTML5 `history` api, aka `pushState`. This module does not support hash based routing.
## Usage
```
$ npm install --save nighthawk
````browser.js`:
```javascript
var Nighthawk = require('nighthawk');// Create your router
var router = Nighthawk();// Register your routes
router.get('/', function(req, res) {
alert('Hi from your nighthawk router!');
});
router.get('/:foo', function(req, res) {
alert('You visited /' + req.params.foo);
});router.listen();
```### Setting A Base Path
Nighthawk supports service applications that are not hosted at the root of your domain via `base`. To set a base path just pass it in to
the router constructor. For example:```javascript
var Nighthawk = require('nighthawk');var router = new Nighthawk({
base: '/foo'
});// Optionally you can also set the base path
// with `router.base('/foo')`.router.get('/bar', function(req, res) {
alert('You are not at /foo/bar');
});
router.listen();
```If have routes which match your base path but require full server rendered page refreshes, you
can use the `reloadOnUnhandled` option to have Nighthawk force the browser to reload from the server with the new
route when the final handler is hit. By setting this option to true, the final handler will force the browser to
reload the page from the server via `window.location = `.### Parsing Querystring's
Nighthawk can setup querystirng parsing for you, just pass the desired parsing funciton as `queryParser`. For example:
```javascript
var router = new Nighthawk({
queryParser: require('querystring').parse// Or for extended parsing like in express
queryParser: require('qs').parse
});
```*Note:* The `parseQuerystring` option is deprecated as of `2.1.0`, and will be removed in `3.0.0`.
### Listen Options
There are a few options you can pass to the `listen` call:
- `popstate`: When `false`, Nighthawk will not listen for `popstate` events
- `interceptClicks`: When `false`, Nighthawk will not listen for link `click` events
- `dispatch`: When `false`, Nighthawk will not process the initial route on listen### Manually change url and run middleware stack
In case you have some event on the page which is not a normal link click or popstate, you can
call `router.changeRoute('/your/url')`, and the Nighthawk instance will run the route processing.
This is helpful for things like form submissions and actions which should prompt users to login.### What happens when `history` is not supported?
It just falls back to basic HTML link behavior. Thats the great thing about this pattern, it builds on top of basic building blocks of the web. Also,
if it is not supported, your route will still run, so you can still use Nighthawk to kick off your application in unsupported browsers.### Why use Nighthawk?
Unlike some other recent front-end routing libraries _(react-router, angular-ui-router & ember router)_ [1](#fn1), this
package requires no special integration points into your links and no special methods to call to change routes. It layers transparently over your existing
application and catches link clicks which cause route changes.*But I have to learn a new library!!* No you dont. This library directly uses the Express router, so if you know Express you already know Nighthawk. All
the middleware patterns you know from Express on the server are valid with Nighthawk. If you load data on the backend with a middleware, you can can use
a module like [`nets`](use://github.com/maxogden/nets) to re-use the same middleware on the front-end.Nighthawk is also relatively small, weighing in at 19kb minified and gzipped. If you are using browserify you are probably already bundling modules like `buffer`
and `url`, so if you don't count those we only add 9kb total. This is a fair bit smaller than other comparable libraries for front-end routing. You
can see for yourself by running `npm run size`, which will open a breakdown of where the file size comes from and display some file size stats.## Run the examples
```
$ npm run example-basic
$ npm run example-basedir
$ npm run example-redirect
```Visit [http://localhost:1234](http://localhost:1234).
## Tests
```
$ npm test
```[1]: In opinionated frameworks this is not really that big of a deal because you are already tied down
to an ecosystem that probably works really well. But in a "pick your own adventure" style application it is much nicer to have less coupling.[npm-image]: https://img.shields.io/npm/v/nighthawk.svg
[npm-url]: https://npmjs.org/package/nighthawk
[downloads-image]: https://img.shields.io/npm/dm/nighthawk.svg
[downloads-url]: https://npmjs.org/package/nighthawk