Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
express-spa-router
Advanced tools
Dan Motzenbecker, MIT License
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.
$ npm install --save express-spa-router
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']}));
FAQs
Express middleware for single page app routing
The npm package express-spa-router receives a total of 0 weekly downloads. As such, express-spa-router popularity was classified as not popular.
We found that express-spa-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.