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

a-route

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

a-route

Express like routing as Custom Element or standalone

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

a-route

Social Media Photo by Jakub Gorajek on Unsplash

Express like routing, as Custom Element or standalone, inspired by page.js.

app API

  • app.get(path:string|RegExp, cb:Function[, cb2, ...]):app to subscribe one or more callbacks for the specified route
  • app.delete(path:string|RegExp, cb:Function[, cb2, ...]):app to unsubscribe one or more callbacks for the specified route
  • app.navigate(path:string[, operation:string = 'push']):void to navigate to the first matching route for the given path. By default, it pushes to the history but it could replace, if the second parameter is the replace string, or ignore.
  • app.param(path:string|RegExp):app to subscribe to a specific parameter regardless of the route
  • app.use(path:string|RegExp):app to subscribe a callback for a specific mount point or all of them

Example

The following is a basic example, also available live.

<script src="//unpkg.com/a-route"></script>

<!-- simply add `is="a-route"` to any link in your page -->
<a is="a-route" href="/test/?query=value">test query</a>

<!-- you can also add `no-propagation`, to stop propagation on click
    or you could add `replace` to replace state instead of pushing it -->
<a is="a-route" href="/test/OK" no-propagation replace>test OK</a>

<!-- unregistered routes will pass through `'*'` handler, if any -->
<a is="a-route" href="/whatever">test 404</a>
// import {app} from 'a-route';
// const {app} = require('a-route');
const {app} = ARoute;

// define routes
app
  .get('/test/?query=:query', function (ctx) {
    console.log(ctx);
    /*
    {
      "path": "/test/?query=value",
      "params": {
        "query": "value"
      }
    }
    */
  })
  .get('/test/:status', function (ctx) {
    console.log(ctx);
    /*
    {
      "path": "/test/OK",
      "params": {
        "status": "OK"
      }
    }
    */
  });

// intercept all unregistered calls
app.get('*',
  function (ctx, next) {
    console.log(ctx);
    /*
    {
      "path": "/whatever"
    }
    */
    next();
  },
  // will receive the ctx object too
  console.error
);

Keywords

FAQs

Package last updated on 16 Sep 2020

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