2015-04-29 35 views
2

首先,我是web框架的新手。我们正在使用流星。 我有一个学生集合:设计一个休息API

Students = new Mongo.Collection('students'); 

目前,我们正在定义一个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'} 
     }; 
    }} 
}); 
}; 

现在,可以有另一个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'} 
     }; 
    } 
}); 

目前我访问它们为

http://localhost:3000/api/getAllStudents 

http://localhost:3000/api/getStudentByName?name='abc' 

我会有更多这样的API的学生(GET,PUT,POST,删除)。另外,我还有更多的资源,比如老师,课程等等。每个资源都会定义一组API。现在,我想以一种整洁的方式来设计我的API(我知道这非常模糊和无组织)。我想最终调用它们作为

http://localhost:3000/api/Students/getAllStudentshttp://localhost:3000/api/Students/getStudentByName?name='abc'

现在,我有一个学生,api.js,我已经把这些2 API和类 - api.js和老师,api.js包含自己各自的API。但是,这太没有结构了。 是否可以使用“名称”这样的命名空间? 任何帮助真的很受欢迎..

回答

5

一个REST API不应该包含动词。该动词由HTTP提供。

// All Students 
GET http://localhost:3000/api/students 

一种可能的方式是提供一种过滤器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 

这是使用一个统一的接口的REST API的美感。现在所有的API调用都可以支持相同的过滤器参数。

以下是在单个字段上进行相等匹配的简单示例,因为我没有对其进行测试,所以可能需要对其进行调整。您可以通过支持多个字段和不同的操作员来改进它,使过滤变得更加智能。

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; 
     } 
    }); 
}); 

注:%3D=

+0

但随后的URL编码,怎么会为同一代码写?是否有一些特定的设计模式需要遵循?或任何具体的参考资料,可以帮助我达到相同的目的。 –

+0

这是一个很好的答案!我打算尽快在Restivus中提供这种支持(它不会在0.7.0发布之后)。我希望你不介意我是否使用这段代码作为参考。 – kahmali

+0

刚刚提交了一个问题,以防您或其他人想在Restivus中讨论对此的支持:https://github.com/kahmali/meteor-restivus/issues/70。如所承诺的,我在此使用您的代码作为参考。只是一个头。 – kahmali