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 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-resty

v0.2.0

Published

Node resty

Downloads

47

Readme

node-resty

Create RESTful APIs using express.

Supports the JSON API standards (http://jsonapi.org/format/#fetching)

Getting started

require node-resty with:

var resty = require('node-resty');

Creating a new resource:

var users = new resty.resource('users');

A best practice is to define the resource name in pluralize.

API

There are few functions that available after creating a new resource instance.

  • get
  • getDetails
  • post
  • patch
  • put
  • delete

All methods are excepting one parameter which is the middleware handler.

Example:

users.get(function(req, res, next) {
    res.json({message: 'Get list of users'});
});

Which defines the path: /users

** patch/put/delete methods defines the routes for a single entity. ** for example:

/users/:id
users.getDetails(function(req, res, next) {
    res.json({message: 'Get list of users'});
});

Defines the path: /users/:X, where the X is the ID of resource model. Please attention that the path here is the singular version of the resource name

It is also possible to run a custom route based on the resource name as a prefix

users.route('count', 'get', function(req, res, next) {
    res.send({message: 'This is a users count route'});
    next();
});

This defines the path: /users/count

You can also pass an object to the third argument instead of a function to set the route with a detail prefix route.

for example:

users.route('count', 'get', {
    detail: true,
    handler: function(req, res, next) {
                 res.send({message: 'This is a users count route'});
                 next();
             }
}
});

This will define the route: GET /users/1/count.

Supported filters:

  • before
  • after

Filters are used to add middleware handlers before or after the main middleware-handler This is good for validations of the params that a client sends or validate that user is authenticated.

In order to make the after filter work you must call the next() method on the main middleware handler.

Example:

users.before('get', function(req, res, next) {
    console.log('before users count');
    next();
});

OR

users.before('get', {
    handler: function(req, res, next) {
        console.log('before users count');
        next();
    }
})

Do no forget the next(); at the end of the middleware so it will jump to the next middleware.

The first param is the path of the route you want to attach the filter to. There is "magic" keywords to help us to simplify the process.

The keywords are:

  • get
  • post
  • patch
  • put
  • delete

When you specify each of those keywords it will bind the filter to the main HTTP METHOD by the keyword name.

If you'll specify a path like count it will attach it to the route /users/count

You can also pass as a second param inside the object (as in example 2) a property named "detail" sets to true to specify the filter on a detail route.

Example:

users.before('count', {
    detail: true,
    handler: function(req, res, next) {
        console.log('before users count');
        next();
    }
})

Which will define a before filter to the route: /users/1/count.

Registering the resource to the application

In order to register the resource to the application and make the routes work all you need to add is:

app.use(users.register());

If you want to add the resource routes under a prefix route it is possible by doing:

app.use('/api', users.register());

This will register the routes under /api prefix, for example:

/api/users
/api/users/1
/api/users/count

Inspiration from: https://github.com/baugarten/node-restful