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

pathways

v0.0.11

Published

Simple HTTP RESTful routing.

Downloads

6

Readme

Pathways

A simple HTTP RESTful router.

Examples

Using string patterns

Provides a simple string pattern to match. The pattern can specify URL parameters in the form of :parameterName and wildcards using *. If the handlerFunction has an argument that matches a URL parameter, the value is passed to the callback. For example, the url /collection/:id has a url parameter :id. If the handler function takes id as a parameter it will automatically be bound i.e. function (id) { }.

Usage:

pathways.get(string, handlerFunction);

Example:

var Pathways = require('pathways');
var http = require('http');

var pathways = Pathways();
var server = http.createServer(pathways);

pathways
.get('/', function () {
   this.response.write('Hello World!');
   this.response.end();
})
.get('/collection/:id', function (id) {
   this.response.write('Hello ' + id + '!');
   this.response.end();
})
.post('/collection/:id', function (id) {
   var data = '';

   this.request.on('data', function (b) {
      data += b;
   });

   this.request.on('end', function () {
      console.log(id, data);
      this.response.end();
   }.bind(this));
});

server.listen(3000);

Using Regular Expressions

This is useful for more complicated matching scenarios. For example, ensuring that a url parameter is only digit characters or a certain length. The result of url.match(RegExp) is passed to the handler callback.

Usage:

pathways.get(RegExp, handlerFunction);

Example:

var Pathways = require('pathways');
var http = require('http');

var pathways = Pathways();
var server = http.createServer(pathways);

pathways
.get(/^\/$/i, function () {
   this.response.write('Hello World!');
   this.response.end();
})
.get(/^\/collection\/(\d+)$/i, function (matches) {
   var id = matches[1];
   this.response.write('Hello ' + id + '!');
   this.response.end();
})
.post(/^\/collection\/(\d+)$/i, function (matches) {
   var id = matches[1];
   var data = '';

   this.request.on('data', function (b) {
      data += b;
   });

   this.request.on('end', function () {
      console.log(id, data);
      this.response.end();
   }.bind(this));
});

server.listen(3000);

Using a function

This is useful if you want to inspect things other than just the URL. You have access to the request and response just like the request handler callback.

Usage:

pathways.get(filterFunction, handlerFunction);

Example:

var Pathways = require('pathways');

var http = require('http');
var pathways = Pathways();
var server = http.createServer(pathways);

var rootRoute = function (cb) {
   cb(this.request.url === '/');
};

var collectionRoute = function (cb) {
   if (this.request.url.indexOf('/collection/') === 0) {
      var id = this.request.url.replace('/collection/', '');
      cb(true, id);
   }
   else {
      cb(false);
   }
};

pathways
.get(rootRoute, function () {
   this.response.write('Hello World!');
   this.response.end();
})
.get(collectionRoute, function (id) {
   this.response.write('Hello ' + id + '!');
   this.response.end();
})
.post(collectionRoute, function (id) {
   var data = '';

   this.request.on('data', function (b) {
      data += b;
   });

   this.request.on('end', function () {
      console.log(id, data);
      this.response.end();
   }.bind(this));
});

server.listen(3000);

Handler callback

The handler callback is invoked for the first registered route that matches via string, function, or regular expression. If no routes match, the server responds with a 404 status code.

pathways.get('/', function () {
   // This is the handler callback
});

The handler callback will be invoked with this set to an object with properties:

{
   request: /* the HTTP request */,
   response: /* the HTTP response */,
   router: /* the pathways router */,
   params: /* the query parameters provided in the HTTP request */
}

The handler will be provided arguments depending on the filter.

Using strings:

Pathways will try to bind URL parameters to the callback functions with the same name.

Using RegExp:

The callback will have the match collection as its first argument.

Using a function:

The callback will be invoked with every argument provided to the filter callback after the first. For example cb(true, 1, 'a', 'b') will invoke the route handler with 3 arguments 1, 'a', and 'b'.