access req in expressjs 3 views

Is it possible to access my query params in my views in ExpressJS 3?

I have a url: http://example.com?search=blah

And in my view I would like to access the search param

I can pass it as a locals but wondering if I can access it directly - my experiments were not successful.

Not looking for the pros and cons of direct access - just want to know if it's possible and how.

Here are a few ways to access req.query from your view:

Set it as a local in the call to render

function(req, res) {
  res.render('myview', {query: req.query});
};

in your view you can access search as query.search.

Set res.locals

function(req, res) {
  res.locals.query = req.query;
  res.render('myview');
};

in your view you can access search as query.search.

Use middleware

This is similar to the previous example but we can use middleware in a reusable way.

function(req, res, next) {
  res.locals.query = req.query;
  next();
};

Any route that uses the above middleware, will have res.locals.query set.


edit

It appears that I misunderstood the question. The intent was to see if the request data could be accessed without use of the above techniques. As far as I know, it can not. Hopefully the above will still be useful for some readers.

I am pretty sure only locals get passed to the view.

Not looking for the pros and cons of direct access - just want to know if it's possible and how.

There are no pros and cons. It's like saying I want to multiply 15 with 0 but I don't want the answer to be apple.

middleware:

function(req, res, next) {
  res.locals.param = req.param;
  next();
};

view:

<%= param.search %>