[英]Design a Rest API
Firstly, I am new to web frameworks. 首先,我是Web框架的新手。 We are using Meteor.
我们正在使用流星。 I have a Students Collection:
我有一个学生集:
Students = new Mongo.Collection('students');
Currently, we are defining a Rest API as: 目前,我们将Rest API定义为:
// Maps to: /api/getAllStudents
Restivus.addRoute('getAllStudents', {authRequired: false}, {
get: function () {
var students = Students.find().fetch();
if (students.length > 0) {
return {status: 'success',count:students.length,data: students};
}
else {
return {
statusCode: 404,
body: {status: 'fail', message: 'Students not found'}
};
}}
});
};
Now, There can be another API getStudentByName as 现在,可以有另一个API getStudentByName作为
// Maps to: /api/getStudentByName by name
Restivus.addRoute('getStudentByName', {authRequired: false}, {
get: function () {
var obj = {};
for(var key in this.queryParams){
var val = this.queryParams[key];
obj[key] = val;
}
var students = Students.find(obj).fetch();
if (student.length > 0) {
return {status: 'success',count: students.length, data: students};
}
return {
statusCode: 404,
body: {status: 'fail', message: 'Students not found'}
};
}
});
Currently I am accessing them as 目前,我以
http://localhost:3000/api/getAllStudents
and 和
http://localhost:3000/api/getStudentByName?name='abc'
I will have many more such API's for Students (Get, put, post, delete). 我将为学生提供更多此类API(获取,放置,发布,删除)。 Also, I have many more resources like Teachers, Classes, etc. For each there will be set of API's defined.
此外,我还有更多资源,如教师,班级等。每一种资源都会定义一组API。 Now, I want to design my API's in a neater way(I know this is very vague and unorganized).
现在,我想以一种更整洁的方式设计我的API(我知道这是非常模糊和无组织的)。 I want to eventually call them as
我想最终称他们为
http://localhost:3000/api/Students/getAllStudents
http://localhost:3000/api/Students/getStudentByName?name='abc'
http://localhost:3000/api/Students/getAllStudents
http://localhost:3000/api/Students/getStudentByName?name='abc'
Right now, I have a Student-api.js where I have placed these 2 API's and classes-api.js and teachers-api.js containing their own respective API's. 现在,我有一个Student-api.js,在其中放置了这2个API,并分别包含了各自的API的class-api.js和Teachers-api.js。 But, that's just too unstructured.
但是,这太无组织了。 Can "something" like namespacing be used?
可以使用诸如命名空间之类的“东西”吗? Any help is really welcomed..
任何帮助都非常欢迎。
A REST API should not contain verbs. REST API不应包含动词。 The verb is provided by HTTP.
该动词由HTTP提供。
// All Students
GET http://localhost:3000/api/students
A possible way is to provide a filter param 一种可能的方法是提供过滤器参数
// Students named Mark
GET http://localhost:3000/api/students?filter=name%3DMark
// Teachers whose last name is Johnson
GET http://localhost:3000/api/students?filter=lastName%3DJohnson
This is the beauty of REST APIs using a uniform interface. 这是使用统一接口的REST API的优点。 Now all your API calls can support the same filter parameter.
现在,您所有的API调用都可以支持相同的过滤器参数。
The following is a simple example for equality matching on a single field that probably needs to be adjusted since I haven't tested it. 以下是一个简单的示例,用于在单个字段上进行相等匹配,由于我尚未对其进行测试,因此可能需要对其进行调整。 You can improve it to make the filtering smarter by supporting multiple fields and different operators.
您可以通过支持多个字段和不同的运算符来改进它,以使过滤更加智能。
var filterableCollections = ['students', 'teachers'];
filterableCollections.forEach(function(collectionName) {
var Collection = new Mongo.Collection(collectionName);
Restivus.addRoute(collectionName, {authRequired: false}, {
get: function() {
var items;
var filter = this.queryParams.filter;
if (filter) {
var fieldNameValue = filter.split('=');
var queryObj = {};
queryObj[fieldNameValue[0]] = fieldNameValue[1];
items = Collection.find(queryObj).fetch();
} else {
items = Collection.find().fetch();
}
var response;
if (items.length > 0) {
response = {status: 'success', count: items.length, data: items};
} else {
response = {
statusCode: 404,
body: {status: 'fail', message: collectionName + ' not found'}
};
}
return response;
}
});
});
Note: %3D
is the URL encoding of =
注意:
%3D
是=
的URL编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.