npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-spa-router

v0.0.8

Published

Express middleware for single page app routing

Downloads

41

Readme

Express Single Page App Router Middleware

Internally reroute non-AJAX requests to your client-side app router.

Dan Motzenbecker, MIT License

@dcmotz

Concept

Let's say you have a modern single page web application with client-side URL routing (e.g. Backbone).

Since views are rendered on the client, you'll likely use RESTful Express routes that handle a single concern and return only JSON back to the client. The app's only non-JSON endpoint is likely the index route (/).

So while /users might return a JSON array when hit via the client app's AJAX call, you'll want to handle that request differently if the user clicks a link from an external site or manually types it in the address bar. When hit in this context, this middleware internally redirects the request to the index route handler, so the same client-side app is loaded for every valid route. The URL for the end user remains the same and the client-side app uses its own router to show the user what's been requested based on the route. This eliminates the tedium of performing this kind of conditional logic within individual route callbacks.

Installation

$ npm install --save express-spa-router

Usage

In your Express app's configuration, place this middleware high up the stack (before router and static) and pass it your app instance:

app.use(require('express-spa-router')(app));

AJAX requests will be untouched, but valid routes called without AJAX will result in the the index route's result being returned. Non-matching routes will be passed down the stack by default and will be end up being handled by whatever your app does with 404s. This can be overridden by passing a noRoute function in the options object:

app.use(require('express-spa-router')(app,
  {
    noRoute: function(req, res, next) {
      //handle unmatched route
    }
  }
));

Express's default static paths are passed along correctly by default (as are /js and /css), but if you use different paths or have additional static files in your public directory, make sure to specify them in the options either via a regular expression or an array of directory names:

app.use(require('express-spa-router')(app, {staticPaths: ['js', 'css', 'uploads']}));

You may also have valid client-side routes that don't exist on the server-side. Rather than having them reach the 404 handler, you can specify them in the configuration options using extraRoutes and passing either a regular expression or an array:

app.use(require('express-spa-router')(app, {extraRoutes: ['about', 'themes']}));

Finally, if you want to route non-AJAX GET requests to certain routes normally, pass paths in the ignore option:

app.use(require('express-spa-router')(app, {ignore: ['api']}));