3

I'm very new to node.js, specifically express.js. I'm working on express.js app to export some rest apis and I found that I have many dup codes that I would like to avoid this issue. I'm seeking a best practice solution. This is my made-up examples:

// app/routes/category.js
exports.create = function (req, res) {
  categoryModel.create(someData, function (error, category) {
    if (Util.isError(error)) {
      res.send(400, error);
    } else {
      res.send(category);
    }
  });
};

// app/routes/product.js
exports.create = function (req, res) {
  productModel.create(someData, function (error, product) {
    if (Util.isError(error)) {
      res.send(400, error);
    } else {
      res.send(product);
    }
  });
};

You can see that both my create routes codes are pretty much the same but just different model (product vs category) my questions are:

1) is there a way to do inheritance in node.js/express.js?

2) is there a way to do interface in node.js/express.js?

3) what is the best way to avoid duplicate code as above samples?

4) I was thinking of it is possible to have parent class and let both category and product classes inherits from parent. I really do not how it should be done in node.js

I read couple of node.js books but I don't see authors mentioned much about how to architect app in correct or at least best practice.

I read about Prototype.js, is it the right path to go?

Any thoughts and helps would be very appreciated!

3
  • 1
    Whatever you do, do NOT use Prototype.js. It modifies built-in prototypes. That's bad. ); Sep 12, 2013 at 23:16
  • so what is your suggestion @EricElliott, will you talk about this on your webcast coming this week?
    – Nam Nguyen
    Sep 12, 2013 at 23:40
  • 1
    I suggest you use the higher order function as suggested by freakish, below. I don't know if his code fits exactly what you need, but the basic idea is that you pass the unique stuff into a function, and that function returns the route handler that will be called. The shared stuff goes inside the higher order function, and that is what facilitates the code reuse. You can then require() that higher order function wherever you need to use it. Sep 13, 2013 at 0:34

1 Answer 1

4

There's no need for inheritance (especially since handlers have to be functions so it's hard for me to see how you can use inheritance here). You can simply create a function generator:

var generate_handler = function(model) {
  return function(req, res) {
    model.create(someData, function (error, obj) {
      if (Util.isError(error)) {
        res.send(400, error);
      } else {
        res.send(obj);
      }
    });
  };
};

and then you do

// app/routes/category.js
exports.create = generate_handler(categoryModel);

// app/routes/product.js
exports.create = generate_handler(productModel);
7
  • thanks @freakish. In production env, is this how people normally do it? I'm so curious because when I read books, I saw that there are a lot of code dup and I'm wondering if there is a better solutions. Obviously I'm very new to Node.js
    – Nam Nguyen
    Sep 5, 2013 at 7:15
  • @NamNguyen I don't know how people normally do it since I've never worked with Node.JS in prod env. But this looks like a sensible solution. Code duplication is always something you should avoid regardless of the language you are using. Also I've used (and seen people using) such solutions in browser's JavaScript.
    – freakish
    Sep 5, 2013 at 7:17
  • I agree that we should always avoid code dup and Node.js world is kinna difference from Java, .NET which is more OOP design. The solution that you suggest to create function generator would do the job for now but it's kinna not OOP in my point of view.
    – Nam Nguyen
    Sep 5, 2013 at 7:21
  • @NamNguyen True, but if it solves your problem (and it is simple and efficient enough) then who cares about OOP? Simplicity uber alles. ;)
    – freakish
    Sep 5, 2013 at 7:26
  • 2
    "Sometimes, the elegant implementation is just a function. Not a method. Not a class. Not a framework. Just a function." - John Carmack Sep 12, 2013 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.