0.0.4 • Published 8 years ago

route-hasher v0.0.4

Weekly downloads
6
License
MIT
Repository
github
Last release
8 years ago

route-hasher

NPM version

Middleware for express or connect that redirects requests to the root path / with url.pathname written into url.hash.

Useful for single page apps with a client-side router e.g. Angular. Since the server is unaware of UI routes outside of /, a direct request to a specific pathname such as /users/hiebj or /account/orders causes a naïve server to respond with 404 Not Found. This middleware instead responds with 302 Found, redirecting such a request to the root path with the route information stored in the URL's hash component:

GET /users/hiebj  
-> 302 Found /#/users/hiebj  
GET /account/orders  
-> 302 Found /#/account/orders  
GET /search?param1=val1&param2=val2  
-> 302 Found /#/search?param1=val1&param2=val2

This way, the route information is handed off to the client-side router, which can use it to render the appropriate view.

Usage

const express = require('express'),  
  hasher = require('route-hasher');  

express()  
  .use(hasher('!'))  
  .use(express.static('.'))  
  .listen(8080);

Options

hashPrefix

string
A string to prepend to url.pathname before writing it into url.hash. For example, if '!' is used (as in the example), a request to /users/hiebj is redirected to /#!/users/hiebj.