javascript - Design a Rest API -


firstly, new web frameworks. using meteor. have students collection:

students = new mongo.collection('students'); 

currently, defining rest api as:

// 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 api getstudentbyname as

// maps to: /api/getstudentbyname 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 accessing them

http://localhost:3000/api/getallstudents 

and

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

i have many more such api's students (get, put, post, delete). also, have many more resources teachers, classes, etc. each there set of api's defined. now, want design api's in neater way(i know vague , unorganized). want call them

http://localhost:3000/api/students/getallstudents http://localhost:3000/api/students/getstudentbyname?name='abc'

right now, have student-api.js have placed these 2 api's , classes-api.js , teachers-api.js containing own respective api's. but, that's unstructured. can "something" namespacing used? welcomed..

a rest api should not contain verbs. verb provided http.

// students  http://localhost:3000/api/students 

a possible way provide filter param

// students named mark http://localhost:3000/api/students?filter=name%3dmark  // teachers last name johnson http://localhost:3000/api/students?filter=lastname%3djohnson    

this beauty of rest apis using uniform interface. api calls can support same filter parameter.

the following simple example equality matching on single field needs adjusted since haven't tested it. can improve make filtering smarter supporting multiple fields , 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 url encoding of =


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -