4

So I'm writing a whole bunch of vendor-specific files in node which all have a similar controller pattern, so it makes sense for me to cut them out and put into a common file.

You can see my common controller file here: https://gist.github.com/081a04073656bf28f46b

Now when I use them in my multiple modules, each consecutively loaded module is overwriting the first. This is because the file is only required once and passed dynamically through to each module on load (this allows me to add extra modules and these modules are able to add their own routes, for example). You can see an example module here: https://gist.github.com/2382bf93298e0fc58599

You can see here on line 53 I've realised that we need to create a seperate instance every time, so I've tried to create a new instance by copying the standardControllers object into a new object, then initialising the new object. This has zero impact on the code, and the code behaves in exactly the same way.

Any ideas guys? I'm in a bit of a jam with this one!

0

1 Answer 1

14

First thing I'd do is try to make things simpler and reduce coupling by invoking the single responsibility principle, et al. http://www.codinghorror.com/blog/2007/03/curlys-law-do-one-thing.html

Put those Schemas into their own files, eg

models/client.js
models/assistant.js
models/contact.js

I've also found that embedded docs + mongoose is generally a PITA. I'd probably promote all those to top level docs.

You don't need to enclose your object's keys in quotes.

routes = {
   list: function() {} // no quotes is aok
}

Also 'list' in typical REST apps is called 'index'. Anyway.

Ok, I'd break this up differently. Since you're requiring stuff from the index.js file in the middleware, they become tightly coupled, which is bad. in fact, I think I'd rewrite this whole thing so it was tidier. Sorry.

I'd probably replace your 'middleware' file with an express-resource controller https://github.com/visionmedia/express-resource (built by author of express). This is a good framework for restful controllers, such as what you're building. The auto-loader is really sweet.

You may also want to look at: http://mcavage.github.com/node-restify/ It's new, I haven't tried it out, but I've heard good things.

Since what you're building is basically an automated mongoose-crud system, with optional overriding, I'd create an express-resource controller as your base

/controllers/base_controller.js

and it might look like

var BaseController = function() {} // BaseController constructor

BaseController.prototype.index = function() {
   // copy from your middleware
}
BaseController.prototype.show = function() {
   // copy from your middleware
}
BaseController.prototype.create = function() {
   // copy from your middleware
}
// etc

module.exports = BaseController

Then I'd do something like:

/controllers/some_resource_controller.js

which might look something like:

var BaseController = require('./base_controller')
var NewResourceController = function() {
    // Apply BaseController constructor (i.e. call super())
    BaseController.apply(this, arguments) 
}

NewResourceController.prototype = new Base()

NewResourceController.prototype.create = function() {
    // custom create method goes here
}



module.exports = NewResourceController

Then to use it, you can do:

var user = app.resource(myResourceName, new ResourceController());

…inside some loop which sets myResourceName to be whatever crud you're trying to set up.

Here's some links for you to read:

Also, it sounds like you're not writing tests. Write tests.

Sign up to request clarification or add additional context in comments.

5 Comments

Wow. One hell of an answer. I'm going to have to go through this piece by piece. Thanks for taking the time! I love stackoverflow.
in the controller of NewResourceController you should write something like Base.call(this) to apply the operations that are performed in the Base-Constructor on the child class. (creation of privileged methods for example)
I'm having a not-so good time trying to integrate your changes into my existing code (to prove to others that this is the way forward): gist.github.com/01534460ecbfc2bb44eb and gist.github.com/a887949c4cdef9c5dae7 for my new implementation... and each controller returns: { flow: undefined }
You're not passing the flow object in? i.e. new Controller(stuffToPassToConstructor). But very seriously: avoid needing to pass it in. The flow object is smelling a lot like a 'god object' en.wikipedia.org/wiki/God_object & stuff like this.flow.model.findById smells like law of demeter violation en.wikipedia.org/wiki/Law_of_Demeter. Perhaps you may need to pass in the model you're working on, but you can configure winston in one file, & require it in the other. the pubsub stuff should listen for events on the Model & could be done better as mongoose post-save middleware.
I'm pretty sure the line: NewResourceController.prototype = new Base() should go before the definition of NewResourceController.prototype.create

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.