Access request params in some module

46 views
Skip to first unread message

krzyszto...@gmail.com

unread,
Nov 6, 2014, 7:09:47 PM11/6/14
to tot...@googlegroups.com
I have some well defined module. I want to have access in it to request.cookie, but I don't want always explicite to write something like framework.module('some_module_name').doWhatIWant(request); especially that this method doWhatIWant can be called not necessarily from Controller, or Middleware where request object maybe hardly accessible.
Any idea on some nice, elegant solution?

Peter Širka

unread,
Nov 7, 2014, 1:17:10 AM11/7/14
to tot...@googlegroups.com, krzyszto...@gmail.com
Hello-uuu,
you have two ways:

1. middleware (async), you can use a global middleware:

framework.middleware('my-module-middleware', function(req, res, next, options) { next() });
framework
.use('my-module-middleware');

2. event (sync) ->

framework.on('request', function(req, res) {});

or

framework.on('request-begin', function(req, res) {});

or

framework.on('controller', function(controller) {});

And I think, that's all.
Thanks.

krzyszto...@gmail.com

unread,
Nov 7, 2014, 3:23:55 AM11/7/14
to tot...@googlegroups.com, krzyszto...@gmail.com
Right, I also started with middleware. I have been done something like:

framework.middleware('my-module-middleware', function(req, res, next, options) {
framework.module('some_module').someObject = req.cookie('some_cookie');
next();
});
I done that to save some request cookie stuff, which I need.
And after that I have helper, or other module when I want to use this
framework.module('some_module).someObject. The thing is that I don't have request in place when I want to use it (I don't want to explicite bind it there). The problem is when save this information in module then for some time for every other request not this particular one I will have this cookie data. There is then a some chance that during some many async. request I will have bad data. Any idea how to sort that in good way? I know that I can can bind request by hard. I can also use some session storage. But maybe some other idea? Maybe some js scope with request access, accessible for every application part - wrapper for nodejs domain (http://nodejs.org/api/domain.html)?

Peter Širka

unread,
Nov 7, 2014, 7:19:35 AM11/7/14
to tot...@googlegroups.com, krzyszto...@gmail.com
You can create a middleware in your module.
I understand a part and I have to know a whole scenario.

I'd like to help you.
Thanks.

krzyszto...@gmail.com

unread,
Nov 8, 2014, 5:02:04 AM11/8/14
to tot...@googlegroups.com, krzyszto...@gmail.com
So, here you have some code sample: https://github.com/KrzysztofWilczek/TotalJSSample

Scenario:
1. We have two users A & B on two different browsers
2. A make request on http://127.0.0.1:8000/set/1
Set -> 1
3. After that A make request http://127.0.0.1:8000/get/
Get -> 1
4. B make request on http://127.0.0.1:8000/get/
Get -> 1
So you can see value set by different user taken from module scope. Value is stored in the same memory space for single module.

Possible solution: always bind current request to module and get cookie data from it, make additional stuff and return value.
So in controllers/main.js do something like:
this.module('strange').bind(req).getValue();
or
this.module('strange').getValue(req);

The thing is, that the best case will be when in middleware we can get, parses and save somewhere cookie data per request and after that I can use this stored value in executed framework.module method in the same request thread.

Question is: how to store it during request thread? Is there any better option to not explicite pass request object?
I'm thinking around options like:
https://www.npmjs.org/package/continuation-local-storage

Peter Širka

unread,
Nov 8, 2014, 5:17:53 PM11/8/14
to tot...@googlegroups.com, krzyszto...@gmail.com
This
this.module('strange').getValue(req);
looks good. Sorry, I don't know to imagine your idea. I have a lot of work currently, so maybe this is the my problem to understand you :-)

Thanks!

krzyszto...@gmail.com

unread,
Nov 10, 2014, 8:04:01 AM11/10/14
to tot...@googlegroups.com, krzyszto...@gmail.com
Yea, but it is something, which I want to avoid.
Anyway I found solution. You can see it on this github repo right now.
I used continuation-local-storage package. Here you can find more about how it works: https://datahero.com/blog/2014/05/22/node-js-preserving-data-across-async-callbacks/
Reply all
Reply to author
Forward
0 new messages