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

arrest-legacy

v1.1.4

Published

REST framework for Node.js, Express and MongoDB

Downloads

55

Readme

arrest

REST framework for Node.js, Express and MongoDB

Arrest lets you write RESTful web services in minutes. It works with Express 4.x, implements simple CRUD semantics on MongoDB and the resulting web services are compatible with the $resource service of AngularJS.

How to Install

npm install arrest

Super Simple Sample

The following sample application shows how to attach a simple REST API to and express application. In the sample, the path /api is linked to a data collection on a MongoDB instance running on localhost:

var arrest = require('arrest')
  , express = require('express')
  , bodyParser = require('body-parser')
  , app = express()

app.use(bodyParser.json());

arrest.use(app, '/api', new arrest.RestMongoAPI('mongodb://localhost:27017', 'data'));

app.listen(3000);

Now you can query your data collection like this:

curl "http://localhost:3000/api"

You can add a new item:

curl "http://localhost:3000/api" -d "name=Jimbo&surname=Johnson"

(for complex objects, just do a POST with a JSON body)

You can query a specific item by appeding the identifier of the record (the _id attribute):

curl "http://localhost:3000/api/51acc04f196573941f000002"

You can update an item:

curl "http://localhost:3000/api/51acc04f196573941f000002" "name=Jimbo&surname=Smith"

And finally you can delete an item:

curl "http://localhost:3000/api/51acc04f196573941f000002" -X DELETE

To use this REST service in an AngularJS application, all you need to do is to include the ngResource service and, in a controller, create a $resource object:

var api = new $resource('/api/:_id', { _id: '@_id' }, {});

$scope.data = api.query();

Default API routes

By default, each time you call arrest.use specifying a different path, the following routes are added to your Express app:

app.get('/path', arrest.RestAPI._query);
app.get('/path/:id', arrest.RestAPI._get);
app.put('/path', arrest.RestAPI._create);
app.post('/path', arrest.RestAPI._create);
app.post('/path/:id', arrest.RestAPI._update);
app.delete('/path/:id', arrest.RestAPI._remove);

Creating a custom API

To create a custom API, start by defining a sub class of RestMongoAPI (or RestAPI if you don't need MongoDB support):

var util = require('util')
  , arrest = require('arrest')
  
function MyAPI() {
  arrest.RestMongoAPI.call(this, 'mongodb://localhost:27017', 'my_collection');
}

util.inherits(MyAPI, RestMongoAPI);

You can now customize, for example, how the queries on the entire collection are performed: the following example checks that a query parameter q is passed to the web service:

MyAPI.prototype._query = function(req, res) {
  if (!req.query.q) {
    arrest.sendError(res, 400, 'q parameter is missing');
  } else {
    this.query({ name: req.query.q}, arrest.responseCallback(res));
  }
}

To add a new web services, modify the routes array, adding the required entries. The array contains objects with to following format:

{
  method: 'get|post|put|patch|delete|any other valid http method',
  mount: '/path/to/the/new/webservice/:with/:needed/:paramenters',
  handler: this.handler_function }
}

The default routes are:

[
  { method: 'get',    mount: '',     handler: this._query },
  { method: 'get',    mount: '/:id', handler: this._get },
  { method: 'put',    mount: '',     handler: this._create },
  { method: 'post',   mount: '',     handler: this._create },
  { method: 'post',   mount: '/:id', handler: this._update },
  { method: 'delete', mount: '/:id', handler: this._remove }
]

For example:

function MyAPI() {
  arrest.RestMongoAPI.call(this, 'mongodb://localhost:27017', 'my_collection');
  this.routes.push({ method: 'get', mount: '/greet/:name', handler: this._hello });
}

util.inherits(MyAPI, arrest.RestMongoAPI);

MyAPI.prototype._hello = function(req, res) {
  res.jsonp({ hello: req.params.name });
}

And, as a last step, remember to use the new API:

arrest.use(app, "/api", new MyAPI());