Search code examples
node.jstestingmocha.jshapi.jssupertest

Created rest ressource in Hapijs


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

Solution

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