koa-rest-mongoose, koa 1.x

2016-08-31 14:58:09 +08:00
 luoyjx

why

期初我就是想找个能根据 model 生成 api 的库,找到了个叫 koa-mongo-rest 的,貌似很久没维护了,现在 koa-router 的版本低了,所以我照着他搞了一个。 如果你们需要的话。

koa-rest-mongoose

mongoose rest generator for koa

安装

Install using npm: npm install koa-rest-mongoose

示例

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

var koaRestMongoose = require('koa-rest-mongoose');

var mongoUrl = '127.0.0.1:27017/koa_rest_mongoose';
var mongoose = require('mongoose');
mongoose.connect(mongoUrl);

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

var model = mongoose.model('user', schema);

var app = koa();

koaRestMongoose(app, router, 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. |

测试

npm test

推荐

sequelize

最后

欢迎 issues 或者 pr :)

2224 次点击
所在节点    Node.js
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/302922

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX