【问题标题】:Hapi.js Content NegotiationHapi.js 内容协商
【发布时间】:2014-09-06 21:44:47
【问题描述】:

我想在我的 hapi 路由中进行内容协商,以便我可以返回客户端可以接受的数据类型。

有了快递,我会做这样的事情

res.format({
  "text/plain": function(){
    res.send("Text Response");
  },

  "text/html": function(){
    res.send("HTML Response");
  },

  "application/json": function(){
    res.json({ message: "JSON Response" });
  },

  "default": function() {
    // log the request and respond with 406
    res.status(406).send("Not Acceptable");
  }
});

是否有内置方法可以使用 hapi 执行此操作?我查看了API docs 并没有看到任何东西。我是否被困在自己的解决方案中?

【问题讨论】:

标签: javascript node.js content-negotiation hapijs


【解决方案1】:

正如@ubaltaci 指出的那样,没有内置方法。

但是有一个 hapi 插件正在开发中 hapi-negotiator 构建在模块 Negotiator 之上。

还没有时间尝试,但很快就会尝试。 (我会更新我的答案)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 2015-08-06
    • 2011-02-05
    • 2012-08-13
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    相关资源
    最近更新 更多