4 ms·
This looks fantastic. The link to the framework itself is https://github.com/walmartlabs/hapi https://github.com/walmartlabs/hapi. In particular, I'm excited a
by peter_l_downs 14y ago
This looks fantastic. The link to the framework itself is https://github.com/walmartlabs/hapi 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);
});
}
- javajosh 14y agoDoing this would be a mistake. You do not want to add web-aware code to a module that is concerned with doing math.
- adamnemecek 14y agoI 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"?
- randallsquared 14y agoHaving 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.
- flaviojuvenal 14y agoThe module doesn't know how to interact with a request. Only with integer variables.
- account_taken 14y ago// 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) { ... }
- account_taken 14y agonm. 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.