0

I'm building a node REST API server built with hapi.js, for a CRUD ressource, and for the Ressource creation, I want to set the location header and status code using created rather than code(201).header('location....

According the current API Documentation,

created(uri) - sets the HTTP status code to Created (201) and the HTTP 'Location' header where: uri - an absolute or relative URI used as the 'Location' header value.

However, when I try to do a post method on my request, BOOM, some exception.

Debug: internal, implementation, error TypeError: Uncaught error: undefined is not a function

suggesting that the property/method doesn't really exist.

(hapi is still evoluting, and most blogpost refer to an old request.reply...)


How should I do then?

Here is the route:

{
    method: 'POST',
    path: '/api/myressource',
     config: {
         handler: function (request, reply) {
             // creation logic
             reply.created("/created/path");
         }
     }
}
AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45

1 Answers1

0

My bad, I should have read a bit more of the documentation...

reply is a callback, not the reply object. (feeling java background...)

So what I needed to do was: reply().created("/created/path");

AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
  • 1
    `reply()` isn't actually a promise, it's callback function, it's called on process.nextTick() though, so you can add headers like created after it's called :) – John Mar 03 '15 at 16:20
  • indeed, still need to make progress on js terminology :) – AdrieanKhisbe Mar 03 '15 at 16:34