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

@sundowndev/router.js

v1.6.2

Published

Simple client sided Javascript routing library.

Downloads

25

Readme

router.js

Simple client sided Javascript routing library for static websites such as documentation or personal website. See it in action here

Features

  • Static & dynamic routing
  • Custom 404 error handling
  • Before and after router middleware
  • Prefixed route paths
  • Route redirection with URL generator

Overview

A simple route

var router = new router();

router.add('default', '/', function () {
    /* do something */
});

A simple route using parameter

router.add('single_category', '/category/:id', function (id) {
  console.log('You requested the category #' + id);
});

Set a callback when returning "route not found"

router.setErrorCallback(function () {
    throw new TypeError('I think there\'s a problem.');
});

Before route middleware

router.before('*', function () {
    /* do something each time the route change */
});

After router middleware

router.run(function () {
    /* do something after running the router */
});

Mapping routes using a route prefix

// This will create two routes under /#/docs prefix
router.map('docs_', '/docs', [
    {
        name: 'intro',
        route: '/',
        callback: function () {
            content.innerHTML = '' +
                '<h1>Introduction</h1>'
            ;
        }
    },
    {
        name: 'get_started',
        route: '/get-started',
        callback: function () {
            content.innerHTML = '' +
                '<h1>Get started</h1>'
            ;
        }
    }
]);

API

Fetch a route by name or path

router.fetchRoute('home'); // or router.fetchRoute('/');

// with parameters
router.fetchRoute('hello', {name: 'Sundown'});

Get the current route

router.route

This will return :

{
    name: [string],
    route: [string],
    callback: [function],
    paramsEnabled: [boolean],
    params: [array]
}

Set and call the not found exception (with example)

var projects = [{title: 'routerjs', description: 'routing library'}];

//overwrite the default not found exception
router.setErrorCallback(function () {
    document.write('Oh no! Page not found.');
});

router.add('projects', '/projects/:title', function (sProjectTitle) {
    //search for the object in array
    let oProject = projects.find(function (project) { sProjectTitle === project.title });

    //if the project does not exist
    if (!oProject) {
        router.notFoundException();
    }
});

Installation (npm)

$ npm i @sundowndev/router.js

Usage

var Router = require('@sundowndev/router.js');

var router = Router();

router.add('home', '/', function () {
    document.write('hello world');
});

router.run();

Installation

  1. Include router.js in or at the end of the
<script src="router.js"></script>

<!-- or via jsdelivr CDN (change the version) -->
<script src="https://cdn.jsdelivr.net/gh/sundowndev/router.js@<VERSION>/dist/router.min.js"></script>
  1. Init the router
<script>
    var router = new router();
</script>
  1. Create some routes
router.add('home', '/', function () {
    document.write('Hello!');
});
  1. Run the router
router.run();

License

This repository is MIT licensed.