You can use req.originalUrl
in express 3.x+. (Older versions can use req.url
from node's http module.) This should produce the raw string, excluding the ?
:
var query_index = req.originalUrl.indexOf('?');
var query_string = (query_index>=0)?req.originalUrl.slice(query_index+1):'';
// 'tt=gg&hh=jj' or ''
Note that if you have a #
to denote the end of the query, it will not be recognized.
If you want to pass the string along to a new URL, you should include the ?
:
var query_index = req.originalUrl.indexOf('?');
var query_string = (query_index>=0)?req.originalUrl.slice(query_index):'';
// '&tt=gg&hh=jj' or ''
res.redirect('/new-route'+query_string);
// redirects to '/newroute?tt=gg&hh=jj'