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

trout

v0.2.0

Published

Semantic Node router, with nested groups and organized parameters.

Downloads

8

Readme

Build Status

trout routing library for node.js

Routing should be simple and semantic. Trout has a very easy way to set up routes and route groupes. It provides different ways of setting route parameters in order to organize your URLs to explicitly define RESTful resources and collections. If your URL scheme is not following REST intentionally, then it still allows simple definitions of route parameters.

Design considerations for trout

Regular Expressions.

Regex is great but to us it should not belong in our URLs. Trout attempts to be as minimalistic yet semantic and logical as possible. This should be a good balance.

Change the default HTTP server listener.

trout is designed to be tiny and unintrusive so you can use it with plain Node and it just adds a few attributes to the request object simplifying development.

Getting Started

Sample Application

var http = require('http');
var router = require('trout');

router.get('/home/:name', function(req, res) {
     res.write("Hello there, " + req.params.name);
     res.end();
});

var handler = function(req, res) {
     router.pass(req, res);
};

var app = http.createServer(handler);
app.listen(8080);

Powerful features

RESTful parameter definitions

You can explicitly define parameters as part of a resource or a collection. Of course if you get lazy or you don't necessarily need a RESTful approach to your application you can just normal parameters.

  • /c:[collectionName] - Accessed by req.params.collections.[collectionName]

  • /r:[resourceName] - Accessed by req.params.resources.[resourceName]

  • /:[normalParameter] - Accessed by req.params.[resourceName]

Route Grouping

trout allows you to group routes together. In this example, you can add further paths to urls starting with "/marbles".

trout also supports arbitrary group nesting, so here we can add other routes that pertain to "/marbles/rare/" such as "price".

router.group('/marbles', function() {
    router.group('/rare', function() {
        router.get('/price/r:marbleName', function(req, res) {
            res.write("The price of " + req.params.resources.marbleName + " is one milllion dollars!");
            res.end();
        });
    })
    
    router.get('/r:marbleName', function(req, res) {
        res.write("Oooh I really like your " + req.params.resources.marbleName + " marble!");
        res.end();
    })
});

Some Examples to Get You Started

trout is designed for powerful semantic RESTful API development. Here's an example.

router.put('/home/:name/c:marbles/r:marbleName', function(req, res) {
   res.write("Oh hey, " + req.params.name + ". You added a: ");
   res.write(req.params.resources.marbleName + " to your " + req.params.collections.marbles + " marbles collection");
   res.end();
});

Sending a PUT request to http://localhost:8001/home/dude/reds/shinyMarble returns:

Oh hey, dude. You added a: shinyMarble to your reds marbles collection.