baseprime / grapnel

The smallest JavaScript router with named parameters, HTML5 pushState, and middleware support
http://grapnel.js.org
468 stars 40 forks source link

added query string support #48

Closed arisAlexis closed 7 years ago

arisAlexis commented 9 years ago

I wanted support for this in my project to make the logic a bit closer to express for example so that there is a req.query object with passed in parameters in the url. I didn't want the order of the parameters to matter for some routes and also including stuff like a token in a route param feels better in a query string.

I made sure all tests pass and added a new test. (it works side by side with named params).

Atom removed some trailing spaces automatically idk if you want to keep them.

Also, this is my first pull request ever , hope it was helpful :)

gopeter commented 8 years ago

:+1: It would be great if this could be merged right now!

arisAlexis commented 8 years ago

@gopeter dirty fix I am using right now

you add it as middleware this._router.add('search/:query',this._getQuery,(req)=>new SearchView({data:{query:req.query}}));

then then use this function

_getQuery(req,event,next) { req.query={}; let queryString=req.params.query.replace('?',''); queryString.split('&').forEach((q)=>{ const name=q.split('=')[0]; let value=q.split('=')[1]; if (value==='true' || value==='false') { req.query[name]=JSON.parse(${value}); } else { req.query[name]=JSON.parse("${value}"); } }); next(); }

so in effect you only put a slash and use the query as a parameter and you get a req.query object

arisAlexis commented 8 years ago

Github reports of conflicts but I am not sure if this will ever be merged.