# yarm

> Yet Another REST Middleware for express and mongoose

Latest version **0.4.0** (published 2016-01-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install yarm
pnpm add yarm
yarn add yarm
bun add yarm
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2016-01-14 |
| First published | 2013-07-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Nicolas Joyard |
| Maintainers | njoyard |
| Keywords | rest, restful, express, mongoose |

## Links

- npm: https://www.npmjs.com/package/yarm
- Repository: https://github.com/njoyard/yarm
- Homepage: http://yarm.njoyard.fr
- Issues: https://github.com/njoyard/yarm/issues
- npm.io page: https://npm.io/package/yarm

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [@temporal-architect/claude-plugin](https://npm.io/package/@temporal-architect/claude-plugin.md) — 335 weekly downloads

## Recent versions

- 0.4.0 (latest) — 2016-01-14
- 0.3.7 — 2014-05-05
- 0.3.6 — 2014-04-23
- 0.3.5 — 2014-03-06
- 0.3.4 — 2014-03-01
- 0.3.3 — 2014-02-15
- 0.3.2 — 2014-01-30
- 0.3.0 — 2014-01-25
- 0.2.0 — 2013-08-11
- 0.1.3 — 2013-08-04
- 0.1.1 — 2013-08-03
- 0.1.0 — 2013-08-03
- 0.0.6 — 2013-08-02
- 0.0.5 — 2013-08-01
- 0.0.4 — 2013-08-01
- … 3 more at https://npm.io/package/yarm/versions

## README

yarm
====

*Yet Another REST Middleware for node.js, Express and mongoose.*

Master branch: [![Build Status](https://travis-ci.org/njoyard/yarm.png?branch=master)](https://travis-ci.org/njoyard/yarm)

Development branch: [![Build Status](https://travis-ci.org/njoyard/yarm.png?branch=devel)](https://travis-ci.org/njoyard/yarm)



## Installation

Use npm to install yarm, or add yarm to your package.json dependencies.

```
$ npm install yarm
```

yarm has no dependencies, however it is intended to be used with Express and will have additional features if mongoose is present.



## Usage

Below is a short introduction to yarm usage, see the [complete documentation][doc] for more information.

### Basics

Use yarm as any other Express middleware.

```javascript
var app = require("express")();
var yarm = require("yarm");

app.use("/rest", yarm());
app.listen(80);
```


### Serving native javascript resources

Use `yarm.native()` to serve native Javascript objects or arrays.

```javascript
var app = require("express")();
var yarm = require("yarm");

app.use("/rest", yarm());

yarm.native("me", {
  name: "Alice",
  age: 30
});

yarm.native("friends", [
  "Bob",
  "Charlie"
]);

app.listen(80);
```

```
$ curl http://localhost/rest/me
{
  "name": "Alice",
  "age": 30
}

$ curl http://localhost/rest/me/name
Alice

$ curl http://localhost/rest/friends
{
  "_count": 2,
  "_items": [ "Bob", "Charlie" ]
}

$ curl http://localhost/rest/friends/1
Charlie
```

Head on to the documentation on [Native resources][doc-native] for more details.


### Serving mongoose models and aggregates

When mongoose is available, you can use `yarm.mongoose()` to serve models.

```javascript
var app = require("express")();
var yarm = require("yarm");
var mongoose = require("mongoose");

app.use("/rest", yarm());

var postSchema = new mongoose.Schema({
  title: String,
  text: String,
  comments: [{
    author: String,
    text: String
  }]
});

var Post = mongoose.model("post", postSchema);

yarm.mongoose("posts", Post);

app.listen(80);
```

```
$ curl http://localhost/rest/posts?skip=10&limit=1
{
  "_count": 42,
  "_items": [
    {
      "_id": "507f191e810c19729de860ea",
      "title": "My 11th post",
      "text": "Hello, World",
      "comments": [
        {
          "author": "Bob",
          "text": "First !"
        }
      ]
    }
  ]
}

$ curl http://localhost/rest/posts/507f191e810c19729de860ea
{
  "_id": "507f191e810c19729de860ea",
  "title": "My 11th post",
  "text": "Hello, World",
  "comments": [
    {
      "author": "Bob",
      "text": "First !"
    }
  ]
}

$ curl http://localhost/rest/posts/507f191e810c19729de860ea/comments/0/text
First !
```

Head on to the documentation on [Mongoose resources][doc-mongoose] for more details.


### Serving custom resources

Use `yarm.resource` to define resources with custom handlers.

```javascript
var app = require("express")(),
var yarm = require("yarm");

yarm.resource("greeting")
  .get(function(req, cb) {
    cb(null, { hello: "world" });
  })
  .sub("french")
    .get(function(req, cb) {
      cb(null, { bonjour: "tout le monde" });
    });

yarm.resource("greeting/pirate")
  .get(function(req, cb) {
    cb(null, { arrrrr: "arrrrrr" });
  });

app.use("/rest", yarm());
app.listen(80);
```

```
$ curl http://localhost/rest/greeting
{
  "hello": "world"
}

$ curl http://localhost/rest/greeting/french
{
  "bonjour": "tout le monde"
}

$ curl http://localhost/rest/greeting/pirate
{
  "arrrrr": "arrrrrr"
}
```

Head on to the documentation on [Custom resources][doc-custom] for more details.


### Extending served resources

yarm allows adding and replacing handlers for any resource or sub-resource. This enables restricting or extending the behaviour of the default native and mongoose resource handlers, as well as defining very complex custom resource hierarchies.

```javascript

yarm.<whatever>()
  .get(function(req, cb) {
    // Override GET handler here
  });

function notAllowed(req, cb) {
  cb(null, "Nope, sorry :(");
}

yarm.native("readonly", myObject)
  .put(notAllowed)
  .post(notAllowed)
  .del(notAllowed)
    .sub("*")
    .put(notAllowed)
    .post(notAllowed)
    .del(notAllowed);

yarm.resource("already/defined/path")
  .get(function(req, cb) {
    // Will not alter 'already' nor 'already/defined' handlers,
    // nor those for 'already/defined/other' if they are defined
  });
```

Head on to the documentation on [Extending resources][doc-extend] for more details.



## Contributing

yarm is published under the terms of the MIT license.  Feel free to report bugs or send pull requests.


[doc]: http://yarm.njoyard.fr
[doc-native]: http://yarm.njoyard.fr/doc-native-resources.html
[doc-mongoose]: http://yarm.njoyard.fr/doc-mongoose-resources.html
[doc-custom]: http://yarm.njoyard.fr/doc-custom-resources.html
[doc-extend]: http://yarm.njoyard.fr/doc-extending-resources.html

---
_Source: https://npm.io/package/yarm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
