Create constants correctly with Mean.js (Node js)

Asked

Viewed 488 times

3

I’m starting to study Mean.js and I was in doubt when the best way to create my application constants. I need to store some constants in the project to be accessible in various parts of the code.

Searching, I found nothing related to constants in the documentation of Mean.js.. On the web, I found these references right here in stackoverflow.

https://stackoverflow.com/questions/15881665/how-to-define-a-constant-and-access-it-everywhere-in-nodejs

https://stackoverflow.com/questions/8595509/how-do-you-share-constants-in-nodejs-modules

Specifically on the second link I found Dominic Barnes' reply very elegant, but in the specific case of Mean.js there is still an abstraction of modules to consider (Mongoose).

How could I create my constants without interfering with the functioning of Mongoose (and without getting ugly, preferably =) )

Note: one thing I would like is that each module file keeps its own constant, so a model of Cars for example, would have the constant

Cars.CARRO_DE_PASEIO

That could have a string, object etc (I’d settle for a string hehe). Something like this...

1 answer

1


In the Nodejs there is global which is a global variable. Similarly window in javascript in browser.

So it is possible to add properties to global and use them wherever you want. You should use them for example global.minhasConstantes = {}; and in that object add key-value to each constant, and not add new constants directly global.constanteA = 'foo'; global.constanteB = 'bar'; for the sake of good practice.

However what I usually do is define the variables when I call the modules. Examples:

in index.js

var modulo = require('modulo')(minhaConstante);

and in the module:

module.exports = function(k){
    return function(){
       // aqui fica o códigoque precisa da constante "k" que é passada para o escopo da função
    }
}

or only:

module.exports = function(k, outroParametro){
   // aqui fica o códigoque precisa da constante "k" que é passada para o escopo da função
}
  • There it is... with Mongoose I don’t use the module.Xports, but Mongoose.model('Meumodel', Meumodelschema); (Of course I defined Meumodelschema before). Can I, within that model file export the globals as well? Will they be read even if I don’t require the model? I posted these questions, but I’ll do the tests myself later, only right now I’m kind of stuck here.

  • @Romulus.Torres export to global will be accessible. It is not the best practice but it is possible. Perhaps the best is to pass within the MeuModelSchema. If you put an example of your code/structure it is easier to help.

  • I’m going to give you the example of the own code... see this example of model exported by Mongoose: https://github.com/meanjs/mean/blob/master/app/models/article.server.model.js Now imagine that I go to the controller of Articles and want to use a constant... I wish it was possible for example to have a constant defined as Articles.ARTIGO_A, which had been defined in my model... anyway it is more or less that

Browser other questions tagged

You are not signed in. Login or sign up in order to post.