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

express-resource-plus

v0.2.1

Published

Provides RESTful routes for resources.

Downloads

10

Readme

Express Resource Plus

Express Resource Plus is a fork of express-resource new providing a few more routes, refactors and an extra feature or two.

**** ONLY compatible with Express 4 ****

Installation

npm: $ npm install express-resource-plus

Usage

In your main application file (i.e. app.js or server.js) just add the following:

var express = require('express'),
    Resource = require('express-resource-plus'), // <- Add this (Resource really isn't needed)
    app = express.createServer();

app.configure(function(){
  // it is important to note that /app directory layout must follow a specific pattern to work properly see [App Layout](#app-layout)
  app.set('app_dir', __dirname + '/app');
  /* ... */
});

App Layout

/app/:version/controllers/:controller_name/index.js routes.js

"What if I want to create a resource on the root path or change the id variable name or define middleware on specific actions?" express-resource-plus handles that by allowing you to set an options property on the controller object like so:

var before = {
  auth : function(req, res, next) {
    // some auth logic
    next();
  },
  owner : function(req, res, next) {
    // resource owner logic
    next();
  }
}

module.exports = {
  options: {
    root: true, // Creates resource on the root path (overrides name)
    name: 'posts', // Overrides module name (folder name)
    id: 'id', // Overrides the default id from singular form of `name`
    before: { // Middleware support
      show: before.auth,
      update: [before.auth, before.owner],
      destroy: [before.auth, before.owner]
    }
  },
  index: function(request, response) {
    response.send('articles index');
  },
  /* ... */
};

Lastly just call app.resource() with your controller name. Nesting is done by passing a function that can call app.resource() for each nested resource. Options can also be passed as the second parameter which override the options set in the controller itself.

app.resource('articles', { version : 'v1'} function() {
  app.resource('comments', { version : 'v1', id: 'id' }); // You can also call `this.resource('comments')`
});

You can also create non-standard RESTful routes.

./controllers/comments/index.js:

module.exports = {
  index: function(request, response) {
    response.send('comments index');
  },
  /* ... */
  search: function(request, response) {
    response.send("Search all comments on this article.");
  },
  reply: function(request, response) {
    response.send("Reply to comment: " + request.params.comment);
  }
};

./app.js:

/* ... */
app.resource('articles', function() {
  this.resource('comments', function() {
    this.collection.get('search');
    this.member.get('reply');
  });
});

Default Action Mapping

Actions are, by default, mapped as shown below. These routs provide req.params.article for the substring where ":article" is and, in the case of the nested routes, req.params.comment for the substring where ":comment" is as shown below:

articles:
index    GET     /articles.:format?
new      GET     /articles/new.:format?
create   POST    /articles.:format?
show     GET     /articles/:article.:format?
edit     GET     /articles/:article/edit.:format?
update   PUT     /articles/:article.:format?
update   PUT     /articles.:format?  ( { where : { username : 'Steve' } })
destroy  DELETE  /articles/:article.:format?
destroy  DELETE  /articles.:format?  ( { where : { username : 'Steve' } })
query    POST    /articles/query.:format?  ( { where : { username : 'Steve' } })
describe HEAD    /articles

article_comments:
index   GET     /articles/:article/comments.:format?
new     GET     /articles/:article/comments/new.:format?
create  POST    /articles/:article/comments.:format?
show    GET     /articles/:article/comments/:comment.:format?
edit    GET     /articles/:article/comments/:comment/edit.:format?
update  PUT     /articles/:article/comments/:comment.:format?
update  PUT     /articles/:article/comments.:format?  ( { where : { username : 'Steve' } })
destroy DELETE  /articles/:article/comments/:comment.:format?
destroy DELETE  /articles/:article/comments.:format?  ( { where : { username : 'Steve' } })
query   POST    /articles/:article/comments/query.:format?  ( { where : { username : 'Steve' } })

Content Negotiation

Content negotiation is currently only provided through the req.params.format property, allowing you to respond accordingly.

License

The MIT License

Copyright (c) 2013 Simon Townsend <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.