0.6.0 • Published 5 months ago

routrrr v0.6.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 months ago

routrrr

simple routing for the browser

Install

npm install routrrr

Example Setup

Javascript

import Routrrr from 'routrrr';

// create an instance
const routrrr = new Routrrr();

// example auth middleware
const auth = function(route) {
  return function(req) {
    return new Promise((resolve, reject) => {
      if (window.authorized) {
        if (route) {
          this.redirect(route, true);
          reject();
        }
        else {
          resolve();
        }
      }
      else {
        if (req.pathname !== '/signin') {
          this.redirect('/signin', true);
          reject();
        }
        else {
          resolve();
        }
      }
    });
  };
};

// setup routes
routrrr
  // add middleware
  .use(req => {
    console.log('use', req.query);
    return Promise.resolve();
  })
  // add basic route
  .get('/', req => {
    console.log('root route', req);
  })
  // add route with middleware
  .get('/home', auth(), req => {
    console.log('home route', req);
  })
  // add route with parameter
  .get('/item/:id', req => {
    console.log('item route', req);
  })
  // add route with parameter in the middle
  .get('/item/:id/edit', req => {
    console.log('edit route', req);
  })
  // add route with optional parameter
  .get('/search/:q?', req => {
    console.log('search route', req);
  })
  // set the default route handler (404)
  .setDefault(req => {
    console.log('fallback route', req);
  })
  // initiate first load
  .init();

// example ajax link setup
document.querySelectorAll('[data-ajax]').forEach(item => {
  item.addEventListener('click', function(e) {
    routrrr.redirect(this.href);
    e.preventDefault();
    return false;
  }, false);
});

.htaccess

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.html [QSA,L]

License

MIT License

0.6.0

5 months ago

0.5.0

2 years ago

0.4.0

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

7 years ago