Reputation: 489
I would like to get request object in my EJS filter. Is it possible?
My filter is quite simple but I need to see in which area is it fired (by url path) to serve correct assets. I believe I can take url from request but how to get it here?
I started to write middleware function which would set interesting value in the function scope, but it would not work because value can be changed by other request before ejs filter will be fired.
I know I could handle it with domains, but I'm not yet familiar with them and even my node version does not support it.
Is there a simple way to take req object inside ejs filter?
Upvotes: 1
Views: 1305
Reputation: 203484
It sounds that your middleware solution was on the right track, but that you used app.locals
instead of res.locals
:
app.use(function(req, res, next) {
res.locals.request = req;
next();
});
app.locals
is an application-wide storage, and would be overwritten by each new request; res.locals
is uniquely scoped to each request.
If you want to use request
in your filter, you're still going to have to pass it as an argument:
<%=: myvar|myfilter:request %>
Obviously, instead of passing the entire request object, you can use just the .url
property if that's the only thing you need in your filter.
Upvotes: 2