express - append (query string) parameters to the current url -


in express,

suppose i'm @ http://localhost:3000/search?q=foo&sort=asc

in template, how can print link (say next pagination link) additional parameters:

search.dust

<a rel="next" href="{! append page=2 !}">next results</a> 

--

of course, could:

<a rel="next" href="{currenturl}&page=2">next results</a> 

but not work when i'm @ http://localhost:3000/search, because of ?/& issue.

thank you

i made dust helper this. called {@query} , here signature:

{@query string="que=ry&str=ing"/} 

it merges que=ry&str=ing actual req.query parameters, in previous example on http://localhost:3000/search?q=foo&sort=asc:

<a rel="next" href="?{@query string="page=2"/}">next</a> 

will output:

<a rel="next" href="?q=foo&sort=asc&page=2">next</a> 

--

the implementation followed (inside middleware have access req.query):

var dust = require('dustjs-linkedin'); var _ = require('underscore'); var qs = require('querystring'); app.use(function(req, res, next) {   //   // query helper dust   //   // merge querystring parameters current req.query   //   // suppose on localhost:3000/search?q=foo :   //   - {@query string=""/} output q=foo   //   - {@query string="bar=baz"/} output q=foo&bar=baz   //   - {@query string="q=fooo&bar=baz"/} output q=fooo&bar=baz (notice fooo takes precedence)   //   dust.helpers.query = function (chunk, ctx, bodies, params) {     var str = dust.helpers.tap(params.string, chunk, ctx);      // parse string="" parameter     var o = qs.parse(str);      // merge req.query     o = _.extend({}, req.query, o);      return chunk.write(qs.stringify(o));   }    next(); }); 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -