Express:如何实现JSON API

Express:如何实现JSON API,json,express,json-api,Json,Express,Json Api,我有一个非常基本的express服务器,它只是通过mongoose为webapps提供模型数据。现在,由于在我的框架(ember)中,JSON-API是用于请求模型数据的新默认适配器,我想知道如何以尊重JSON API规范的方式为我的路由实现响应 我目前在快车上的路线是这样的 router.get('/:id', function(req, res, next) { postModel.find({ //Mongoose functions _id: req.params.id

我有一个非常基本的express服务器,它只是通过mongoose为webapps提供模型数据。现在,由于在我的框架(ember)中,JSON-API是用于请求模型数据的新默认适配器,我想知道如何以尊重JSON API规范的方式为我的路由实现响应

我目前在快车上的路线是这样的

router.get('/:id', function(req, res, next) {
  postModel.find({ //Mongoose functions
    _id: req.params.id
  }, function(err, doc) {
    res.json({ //returning the doc
      data: doc //within 'doc', the 'type'-key must be added
    });
  });

});
我必须在每个响应对象中包含一个“type”键,以便响应对象如下所示:

{
  data:{
    type:"post",
    id:"123",
    title:"test"
  }
}

我只是遇到了同样的问题。有一个npm软件包,但我还没有使用它。他们解决问题的方法实际上是通过扩展DS.RESTAdapter回到余烬数据端。在那里,它们覆盖findQuery函数

findQuery: function(store, type, query) {
    var data = { query: query };
    return this.ajax(this.buildURL(type.typeKey), 'POST', { data: data });
  }
但这并不是很优雅,因为它需要对每种数据类型进行自定义。你可以看到这个例子

希望有帮助。

尝试使用精益()


这里有一个更好的答案。express中的我的请求处理程序:

router.get('/:id', function(req, res, next) {
  Lookup.findById(req.params.id, function(err, result) {
    if (err) {
      res.send({
        error: err
      });
    } else {
      res.send(to_jsonapi(result, 'lookup'));
    }
  });
});
这将调用一个实用函数,该函数将mongoose结果转换为有效的jsonapi结果。您只需要使用结果加上“type”值来调用它

function to_jsonapi(result, type) {
  datajson = [];
  if (Array.isArray(result)) {
    result.forEach(function(item) {
      datajson.push({
        "type": type,
        "id": item._id,
        "attributes": item
      });
    });
  } else if (typeof result === "object") {
    // Happens when there is only one item
    datajson.push({
      "type": type,
      "id": result._id,
      "attributes": result
    });
  } else {
    datajson.push({
      "type": type
    });
  }
  return {
    "data": datajson
  };
}

这不是完美的,但应该让你的道路

如果您在服务器端实现jsonapi,因为它将由您的Ember.js数据自动解析,那么我建议您查看Ember.js提供的不同序列化程序。这将解决问题。因为客户端的工作是以后端提供的任何方式解析数据,而不是我们必须使后端与客户端兼容:)

我不知道express。您的get呼叫应该是:“/[type]/[id]”。post模型的示例:get with/posts/123,返回的对象应该如下所示:数据:{type:“posts”,id:“123”,属性:{title:“test”}类似于json api规范:是的,我知道这些规则。由于express module/middleware的概念,只要在主文件中正确地包含此文件,就可以使用部件
('/:id',
。总之,我的(主)问题是我不知道如何添加类型:“posts”将键值对放入
doc
-var中,这是mongoose文档请求的结果。我想知道是否有一种简便的方法可以在mongodb中查找记录/文档,mongoose已经与jsonapi(带有“type”-字段等)相对应。哦,对不起,我不理解这一点(如上所述,我不知道express/mongoose).但这个github问题可能会有帮助:关于这个问题有什么新的想法吗?有一个路由器助手:但我不确定它是否得到了积极的开发。也许还有其他解决方案得到了很好的支持?谢谢你的回答,你找到了新的信息吗?我发现的json api包没有很好地工作。
function to_jsonapi(result, type) {
  datajson = [];
  if (Array.isArray(result)) {
    result.forEach(function(item) {
      datajson.push({
        "type": type,
        "id": item._id,
        "attributes": item
      });
    });
  } else if (typeof result === "object") {
    // Happens when there is only one item
    datajson.push({
      "type": type,
      "id": result._id,
      "attributes": result
    });
  } else {
    datajson.push({
      "type": type
    });
  }
  return {
    "data": datajson
  };
}