Node.JS - Using a prototype in a module

So, I write a whole bunch of files related to specific manufacturers in node, which all have a similar controller template, so it makes sense for me to cut them out and paste them into a shared file.

Here you can see my general controller file: https://gist.github.com/081a04073656bf28f46b

Now, when I use them in my several modules, each sequentially loaded module overwrites the first one. This is due to the fact that the file is required only once and is dynamically transferred to each module at boot (this allows me to add additional modules, and these modules can add their own routes, for example). Here you can see an example module: https://gist.github.com/2382bf93298e0fc58599

You can see here on line 53. I realized that every time you need to create a separate instance, so I tried to create a new instance by copying the standardControllers object to a new object and then initializing a new object. This has zero effect on the code, and the code behaves exactly same.

Any ideas guys? I am in traffic with this!

+4
source share
1 answer

The first thing I would like to do is try to simplify the situation and reduce the interaction, referring to the principle of shared responsibility, etc. http://www.codinghorror.com/blog/2007/03/curlys-law-do-one-thing.html

Put these schemas in your own files, e.g.

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

I also found that inline docs + mongoose are usually PITA. I would probably promote all those who were in the top-level documents.

You do not need to insert object keys in quotation marks.

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

Also, the "list" in typical REST applications is called "index". Anyway.

Well, I will break it differently. Since you need the material from the index.js file in the middleware, they become closely related, which is bad. In fact, I think I would rewrite it all to be more neat. Unfortunately.

I would probably replace your middleware file with an express resources controller https://github.com/visionmedia/express-resource (created by express author). This is a good foundation for quiet controllers such as what you are building. The autoloader is really sweet.

You can also see: http://mcavage.github.com/node-restify/ This is new, I have not tried it, but I heard good things.

Since you are building a mostly automated mongoose-crud system with optional redefinition, I would create an express resource controller as a 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 would do something like:

 /controllers/some_resource_controller.js 

which might look something like this:

 var BaseController = require('./base_controller') var NewResourceController = function() { // Apply BaseController constructor (ie 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 that sets myResourceName like any pile you are trying to configure.

Here are some links for you:

Also, it looks like you are not writing tests. Write tests.

+11
source

Source: https://habr.com/ru/post/1397463/


All Articles