News for hackers. Stuff that matters.
Re:Hapi, new Node.js framework (a prologue) by peter_l_downs on Saturday February 02, 2013 @09:31PM (#5158303)
This looks fantastic. The link to the framework itself is https://github.com/walmartlabs/hapi.

In particular, I'm excited about the way endpoints are registered:

  // Define the route
  var hello = {
      handler: function (request) {

          request.reply({ greeting: 'hello world' });
      }
  };

  // Add the route
  server.addRoute({
      method: 'GET',
      path: '/hello',
      config: hello
  });
This makes it extremely easy to expose API endpoints to internal modules. Let's say your company has a module related to simple math operations -- exposing this externally is as simple as adding a 'handler' method.

  var SimpleMath = new function() {
    var module = this;
    module.divide = function(a, b) {
      return a  / b;
    };
    module.multiply = function(a, b) {
      return a * b;
    };
    
    // Now add the external handler.
    module.handler = function(request) {
      // somehow access two variables, a and b
      request.reply({
        product: module.multiply(a, b);
        quotient: module.divide(a, b);
      });
  }
Re:Hapi, new Node.js framework (a prologue) by javajosh on Sunday February 03, 2013 @12:49AM (#5158805)
Doing this would be a mistake. You do not want to add web-aware code to a module that is concerned with doing math.
Re:Hapi, new Node.js framework (a prologue) by adamnemecek on Sunday February 03, 2013 @04:13AM (#5159167)
I believe that peter_l_downs was using this exotic literary device called an "example". When you read about class inheritance, was your first reaction "This is useless, why would I ever want to have a Dog class which is based on an Animal class"?
Re:Hapi, new Node.js framework (a prologue) by randallsquared on Sunday February 03, 2013 @09:51AM (#5159807)
Having an internal module know how to interact with a request seems to me to be the point of the example, and I also disagree with it, as a rule.
Re:Hapi, new Node.js framework (a prologue) by flaviojuvenal on Sunday February 03, 2013 @10:30AM (#5159900)
The module doesn't know how to interact with a request. Only with integer variables.
Re:Hapi, new Node.js framework (a prologue) by account_taken on Sunday February 03, 2013 @05:31PM (#5161491)
// Add the route server.addRoute({ method: 'GET', path: '/hello', config: hello });

I'm sorry but how is that different than

    app.get('/hello', require('simplemath'));
where simplemath is

    module.exports = function(req, res) { ... }
Re:Hapi, new Node.js framework (a prologue) by account_taken on Sunday February 03, 2013 @05:34PM (#5161505)
nm. I didn't see the example was returning two properties. Still, that is relatively easy to do. I have a middleware that does that and it's very short not yet another framework.