Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

connect-pushstate

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-pushstate

Connect middleware that rewrites select requests to the site root, allowing your client-side pushstate router to handle them

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

connect-pushstate

Connect middleware that rewrites select requests to the site root, allowing your client-side pushstate router to handle them.

Requests including a file extension are left untouched so site assets like your images, stylesheets, and JavaScripts will load unaffected, while requests without a file extension, presumably pages or actions within your site, are rewritten to point at the root, with the original URL intact.

You can also specify regular expressions to specifically allow or disallow additional paths from being affected.

This functionality is commonly needed by single page web-apps.

Getting Started

Install package

npm install connect-pushstate --save

Load the middleware by adding the following line of JavaScript.

var pushState = require('connect-pushstate');

Add the pushState middleware call to your server definition, amongst your other middleware.

Options

The pushState method accepts an options object as an optional parameter with the following properties.

  • root - The location where requests will be rerouted to. Defaults to '/'. e.g. pushState({ root: '/foo' })
  • allow - A pattern that will allow requests matching it to pass through without being redirected. e.g. pushState({ allow: '^/api' }) You might need this option if your client app and API are on the same server.
  • disallow - A pattern that will disallow requests matching it to pass through without being redirected. e.g. pushState({ disallow: '^/version/1.2.3' })
'use strict';

var connect = require('connect');
var morgan = require('morgan');
var serveStatic = require('serve-static');
var pushState = require('connect-pushstate');
var port = process.env.PORT || 3000;

var app = connect()
.use(pushState())
.use(serveStatic('www/'))
.use(morgan('dev'))
.listen(port, function() {
	console.log('Application server stated on port', port);
});

Note that serve-static is needed in order to actually serve your files.

Examples

For a quick demo, see the examples directory, or run the test suite.

cd examples
node server.js

Tests

Execute the test suite

npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 1.1.0 Add "disallow" option. Add grunt package to devDependencies.
  • 1.0.0 Introduce options object. Add "allow" option. Simplify directory structure. Update dependencies. Other minor housekeeping.
  • 0.1.0 Initial release

Keywords

FAQs

Package last updated on 21 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc