What is the Express way of sharing an object containing a number of constants, etc. and let all middleware have access to them?

30 views
Skip to first unread message

Behrang Saeedzadeh

unread,
Aug 21, 2015, 3:55:38 AM8/21/15
to Express
Looks like 'locals' is not accessible by Middleare.

What's the proper way of sharing an object containing some constants/configurations of my app and let all middleware have access to them?

Thanks in advance,
Behrang

Jason Crawford

unread,
Aug 21, 2015, 11:43:45 AM8/21/15
to expre...@googlegroups.com
If you are writing the middleware yourself, you should just let it take parameters, and pass those to the middleware when you set it up.

-Jason

--
Blog: http://blog.jasoncrawford.org  |  Twitter: @jasoncrawford



--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/d/optout.

Behrang Saeedzadeh

unread,
Aug 21, 2015, 7:44:21 PM8/21/15
to Express
I am actually looking for a way to make the object accessible to all middleware in a DRY way.

Somehow similar to the Servlet specs context-params:

    <context-param>
        <param-name>aConstant</param-name>
        <param-value>aValue</param-value>
    </context-param>


which can then be accessed like this from all servlets:

getServletContext().getInitParameter(“aConstant”);

Jason Crawford

unread,
Aug 21, 2015, 7:47:35 PM8/21/15
to expre...@googlegroups.com
Well, if this is all code you're writing for one server, you can always create a “context” module, put values there, and then have all your middleware modules require that.

-Jason

Angel Java Lopez

unread,
Aug 21, 2015, 7:50:55 PM8/21/15
to expre...@googlegroups.com
An idea:

var config = require('./config');

where you have the constants, etc....

and access it from your middleware. The problem: each middleware should know where is the required file (in the above code, it is in the same directory of the program that makes the require).

Another option: write a module like the above one, but install it in your node_modules, referring it from your package.json

But I prefer this one:

- Write a middleware that enrich the req object, something like

funcion (req, res, next) {
   req.context = require('./context');
   next();
}

and add it as your FIRST middleware in the chain

Angel "Java" Lopez
@ajlopez

Behrang Saeedzadeh

unread,
Aug 23, 2015, 12:27:52 AM8/23/15
to Express
Thanks Angel.

I like your last suggestion better than other available options.

Cheers,
Behrang

Lamzo Enzo

unread,
Aug 23, 2015, 11:31:08 PM8/23/15
to Express
Or you could just export your const / config as a module and then require it whenever you need it.
Reply all
Reply to author
Forward
0 new messages