Node.js CompoundJS:从外部REST API创建模型

Node.js CompoundJS:从外部REST API创建模型,node.js,api,rest,compoundjs,railway.js,Node.js,Api,Rest,Compoundjs,Railway.js,我有一个外部RESTful端点,我正试图在我的CompoundJS应用程序中构建一个模型。有几件事我有点困惑: 如何基于外部RESTful API构建模型(大多数模型似乎都以数据库为中心) 访问这个RESTful API的最佳方式是什么 现在,我正在这样做: var options = { host: 'api.local', port: 80, path: '/users', method: 'GET', headers: { 'Content-Type': 'ap

我有一个外部RESTful端点,我正试图在我的CompoundJS应用程序中构建一个模型。有几件事我有点困惑:

  • 如何基于外部RESTful API构建模型(大多数模型似乎都以数据库为中心)
  • 访问这个RESTful API的最佳方式是什么
  • 现在,我正在这样做:

    var options = {
      host: 'api.local',
      port: 80,
      path: '/users',
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    };
    
    var http = require('http');
    http.get(options, function(res) {
      var output = "";
      res.on('data', function(chunk) {
        output += chunk;
      });
    
      res.on('end', function() {
        var obj = JSON.parse(output);
        send({code: 200, data: obj});
      });
    });
    
    obj
    确实返回了我的所有用户数据(但使用JSON)。返回的JSON如下所示:

    {
      time: integer,
      count: integer,
      data: [{
        userid: string,
        name: string,
        email: string
      }, {
        userid: string,
        ...
      }]
    }
    
    JSON很棒,但我很想把它加载到一个模型中,所以我想我应该有一个外部模型,它包含
    time
    (使用integer类型)、
    count
    (使用integer类型)和
    data
    (使用数组类型)。然后我有一个内部模型(我们称之为
    User
    ),它包含
    userid
    (带类型字符串)、
    name
    (带类型字符串)和
    email
    (带类型字符串)。然后,我必须将
    数据
    与许多
    用户
    模型联系起来。我希望能找到一些类似的方法

    那么,如何基于这个RESTful端点的返回构建一个模型,从端点获取数据的最佳方式是什么?我当前的代码是否高于抓取数据的最佳路径,或者我是否可以使用在
    Schema.js
    中创建的模型设置一种更干净的方法?理想情况下,我能够在
    Schema.js
    中创建一个模型,用RESTful API钩住它,只需执行正常的GET/POST操作


    任何帮助都将不胜感激

    我提出的当前工作解决方案。。。看起来很管用,但看起来有点粗糙。如果您看到TODO评论,这就是我希望能够做到的。。。我只是想知道在这一点上是否可能。这让我能够控制用户模型显示/隐藏的内容,所以我更高兴了

    Schema.js

    var User = describe('User', function() {
      property('userid', String);
      property('name', String);
      property('email', String);
      set('restPath', pathTo.users);
    });
    
    var Hash = describe('Hash', function () {
        property('time', Number);
        property('count', Number);
        property('data', new Array());
        set('restPath', pathTo.hashes);
    });
    
    var options = {
      host: 'api.local',
      port: 80,
      path: '/users',
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    };
    
    var http = require('http');
    http.get(options, function(res) {
      var output = "";
      res.on('data', function(chunk) {
        output += chunk;
      });
    
      res.on('end', function() {
        var obj = JSON.parse(output);
        var data = {};
        data.msec = obj.msec;
        data.count = obj.count;
        data.users = [];
        // TODO: Ideally, this is what I would like to do
        // var hash = new Hash(obj);
        for (var i = 0; i < obj.data.length; i++) {
          var user = new User(obj.data[i]);
          data.users.push(user);
        }
        send({code: 200, data: data});
      });
    });
    
    哈希_controller.js

    var User = describe('User', function() {
      property('userid', String);
      property('name', String);
      property('email', String);
      set('restPath', pathTo.users);
    });
    
    var Hash = describe('Hash', function () {
        property('time', Number);
        property('count', Number);
        property('data', new Array());
        set('restPath', pathTo.hashes);
    });
    
    var options = {
      host: 'api.local',
      port: 80,
      path: '/users',
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    };
    
    var http = require('http');
    http.get(options, function(res) {
      var output = "";
      res.on('data', function(chunk) {
        output += chunk;
      });
    
      res.on('end', function() {
        var obj = JSON.parse(output);
        var data = {};
        data.msec = obj.msec;
        data.count = obj.count;
        data.users = [];
        // TODO: Ideally, this is what I would like to do
        // var hash = new Hash(obj);
        for (var i = 0; i < obj.data.length; i++) {
          var user = new User(obj.data[i]);
          data.users.push(user);
        }
        send({code: 200, data: data});
      });
    });
    
    var选项={
    主机:“api.local”,
    港口:80,
    路径:'/users',
    方法:“GET”,
    标题:{
    “内容类型”:“应用程序/json”
    }
    };
    var http=require('http');
    get(选项、函数(res){
    var输出=”;
    res.on('data',函数(块){
    输出+=块;
    });
    res.on('end',function(){
    var obj=JSON.parse(输出);
    变量数据={};
    data.msec=obj.msec;
    data.count=obj.count;
    data.users=[];
    //托多:理想情况下,这就是我想做的
    //var散列=新散列(obj);
    对于(变量i=0;i