简体   繁体   中英

Hapi.js Content Negotiation

I want to do content negotiation in my hapi routes to I can return the type of data my client can accept.

With express I would do somthing like this

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

Is there a built in way to do this with hapi? I looked through the API docs and didn't see anything. Am I stuck rolling my own solution?

As @ubaltaci pointed, there is not built in way.

However there is a hapi plugin in development hapi-negotiator build on top of module Negotiator .

Haven't time to try it yet, but will do soon. (I'll update my answer then)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM