duyet / koa-mongodb-rest

Rest API generation for Koa

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Koa mongo REST [NPM version]

Easy REST api for koa server

NPM

Installation

Install using npm:

npm install koa-mongodb-rest

Usage

Require library

generateApi = require('koa-mongodb-rest');

Create mongoose model

mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/db');

schema = new mongoose.Schema({
  email: String,
  name: String,
  password: String,
  address: String,
  zipcode: Number,
  lists: Array
});

model = mongoose.model('users', schema);

Create server

var koa = require('koa');
var route = require('koa-route');

var app = koa();

//router is required
app.use(router(app));


//add REST routes to your app. Prefix is optional
generateApi(app, route, model, '/api');

app.listen(process.env.PORT || 5000);

Following REST API is now created for you:

HTTP Verb /users /users/:id
GET Get all documents, or documents that match the query.
You can use [mongoose find conditions] (http://mongoosejs.com/docs/queries.html), limit, skip and sort.
For example:
/api/users?conditions={"name":"john"}&limit=10&skip=1&sort=-zipcode
Get the addressed document.
POST Create a new document and send it back. Update the addressed document with specified attributes.
PUT Create a new document and send it back. Replace the addressed document.
DELETE n/a Delete the addressed document.
PATCH n/a Update the addressed document with specified attributes.

About

Rest API generation for Koa


Languages

Language:JavaScript 47.2%Language:CoffeeScript 45.8%Language:Makefile 7.1%