I've been experimenting with the Express Node.js framework. On the face of it, the approach of passing functions to app.VERB
methods seems unusual. In other frameworks I've used (in languages other than javascript), you create a single handler class for each url pattern, with methods representing different HTTP verbs. One advantage is the ability to bundle common functionality in methods of superclasses.
I've tried to replicate this in node, but I'm interested in feedback on whether this seem like a good approach.
(I realise that the naming of "as_view" doesn't make much sense - I just named it that way because I'm used to django, where the term "view" is used to refer to something akin to a controller rather than a template.)
function Controller(verbs) {
for (verb in verbs) {
this[verb] = verbs[verb];
}
}
Controller.prototype.as_view = function() {
var dispatcher = function(req, res) {
var verb = req.method.toLowerCase();
this[verb](req, res);
};
return dispatcher.bind(this);
};
var pageone = new Controller({
get: function(req, res) {
res.send("Get request");
},
post: function(req, res) {
res.send("Post request");
}
});
....
app.all('/pageone', pageone.as_view());
....
route
in the router. expressjs.com/4x/api.html#router.route \$\endgroup\$