Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/developit/resource-router-middleware
:bicyclist: Express REST resources as middleware mountable anywhere
https://github.com/developit/resource-router-middleware
express javascript middleware rest rest-api
Last synced: 29 days ago
JSON representation
:bicyclist: Express REST resources as middleware mountable anywhere
- Host: GitHub
- URL: https://github.com/developit/resource-router-middleware
- Owner: developit
- License: bsd-3-clause
- Created: 2015-03-19T15:23:07.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-01-06T05:52:02.000Z (almost 6 years ago)
- Last Synced: 2024-11-07T06:25:10.280Z (about 1 month ago)
- Topics: express, javascript, middleware, rest, rest-api
- Language: JavaScript
- Homepage: http://npm.im/resource-router-middleware
- Size: 10.7 KB
- Stars: 120
- Watchers: 7
- Forks: 17
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-github-star - resource-router-middleware
README
# resource-router-middleware
[![NPM](http://img.shields.io/npm/v/resource-router-middleware.svg)](https://www.npmjs.com/package/resource-router-middleware)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/developit/resource-router-middleware)> Express REST resources as middleware, mountable anywhere.
## Usage
### In ES6
```js
import resource from 'resource-router-middleware';export default resource({
mergeParams: true,
id : 'user',load(req, id, callback) {
var user = users.find( user => user.id===id ),
err = user ? null : 'Not found';
callback(err, user);
},list({ params }, res) {
res.json(users);
},create({ body }, res) {
body.id = users.length.toString(36);
users.push(body);
res.json(body);
},read({ user }, res) {
res.json(user);
},update({ user, body }, res) {
for (let key in body) {
if (key!=='id') {
user[key] = body[key];
}
}
res.status(204).send();
},delete({ user }, res) {
users.splice(users.indexOf(user), 1);
res.status(204).send();
}
});
```### In ES5
```js
var resource = require('resource-router-middleware');var users = [];
module.exports = resource({
mergeParams: true,
/** Property name to store preloaded entity on `request`. */
id : 'user',/** For requests with an `id`, you can auto-load the entity.
* Errors terminate the request, success sets `req[id] = data`.
*/
load : function(req, id, callback) {
var user = users.filter(function(user){ return user.id===id; })[0];
if (!user) {
callback('Not found');
}
else {
callback(null, user);
}
},/** GET / - List all entities */
list : function(req, res) {
res.json(users);
},/** POST / - Create a new entity */
create : function(req, res) {
var user = req.body;
user.id = users.length.toString(36);
users.push(user);
res.json(user);
},/** GET /:id - Return a given entity */
read : function(req, res) {
res.json(req.user);
},/** PUT /:id - Update a given entity */
update : function(req, res) {
var id = req.params[this.id];for (var i=users.length; i--; ) {
if (users[i].id===id) {
users[i] = req.body;
users[i].id = id;
return res.status(204).send('Accepted');
}
}
res.status(404).send('Not found');
},/** DELETE /:id - Delete a given entity */
delete : function(req, res) {
var id = req.params[this.id];for (var i=users.length; i--; ) {
if (users[i].id===id) {
users.splice(i, 1);
return res.status(200);
}
}res.status(404).send('Not found');
}
});
```