I'm wondering how to access to req/res outside the scope of a controller in sails.js.
I'm writing a helper (kinda a service) that would need to access to req
without having to send it as parameter, so somehow I need it as global value.
NodeJS Domains can do what you're looking for. You could create a middleware/policy that creates a new domain
for each request, and sets the req object as a property on the domain
. You could then just access the property from the domain
object.
That said, this seems like a code smell. You might want to look at converting your service into a Class
and just instantiate it with the req
. Then you could just pass around the service
in your code.