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

flask-router-plus

v0.3.5

Published

Flask-inspired routing system for node and connect. Nice if you just need a routing system without depending on connect, or need routing middleware without all features provided by express.

Downloads

67

Readme

flask-router

Routing system for node.js/connect based on Flask(http://flask.pocoo.org/).

Installation

npm install flask-router

Usage

var http = require('http')
  , router = require('flask-router')()
  , server = http.createServer(router.route);

It can also be used as a connect/express middleware:

var connect = require('connect')
  , app = connect()
  , router = require('flask-router')()
  , app.use(router.route);

Then routes can be added like this:

router.get('/users/<str(max=5,min=2):id>', function (req, res) {
  console.log(req.params.id);
  res.end();
});

router.post('/users/<str(len=7):id>', function (req, res) {
  console.log(req.params.id);
  res.end();
});

router.put('/customers/<id>', function (req, res) {
  console.log(req.params.id);
  res.end();
});

Can assign multiple handler functions to the same rule:

router.get('/get/<uuid:id>'
, function(req, res, next) {
  res.write('part1');
  next();
}, function(req, res, next) {
  res.write('part2');
  next();
});

router.get('/get/<uuid:id>', function(req, res) {
  res.write('part3');
  res.end();
});
// All three handlers will be executed when the url match, so the final
// response will be 'part1part2part3'

Custom parameter parsers can be registered(these are known as 'converters' in Flask/Werkzeug):

router.registerParser('query', function(str) {
  var rv = {};
    , queryParams = str.split('/')
    , i, len, kv, key, value;
  for (i = 0, len = queryParams.length; i < len; i++) {
    param = queryParams[i];
    kv = param.split('=');
    key = kv[0], value = kv[1];
    rv[key] = value;
  }
  return rv;
});

router.get('/queryable/<query:q>', function(req, res) {
  console.log(JSON.stringify(req.params.q));
  res.end();
});
// If '/queryable/gt=5/lt=10/limit=20' was requested,
// the output would be {"limit":"20","gt":"5","lt":"10"}

Can be used to write middlewares, just like express routes:

// anyone can access public files
router.get('/public/<path:file>', function(req, res) {
  res.write(req.params.file);
  res.end();
});

// will match any path that starts with /private
router.all('/private/<path:path>', function(req, res, next) {
  if (req.headers['x-user']) {
    req.loggedIn = true;
    next('route');
  } else {
    next();
  }
});
router.all('/private/<path:path>', function(req, res) {
  res.writeHead(401); // not authorized
  res.end();
});

// the next two handlers will only be executed if the user is
// authorized(in this case, the request must have x-user header)
router.post('/private/addpost/<title>', function(req, res) {
  // req.loggedIn === true
  res.write('post added'));
  res.end();
});

router.get('/private/posts', function(req, res) {
  // req.loggedIn === true
  res.write(db.query('posts')');
  res.end();
});

RegExps can also be used as rules:

router.get(/^\/posts\/(\d+)/i, function(req, res) {
  // Will match /posts/5 or /POSTs/32422
  // captured text can be accessed by index on req.params
  console.log('Id:', req.params[0])
  res.end()
})

See tests for more examples.