Querystrings?

40 views
Skip to first unread message

Garrett Wilkin

unread,
Aug 31, 2012, 12:51:20 AM8/31/12
to flati...@googlegroups.com
Hello there,

How do I define routes in such a way that I'm handling query strings?  For example, I have this route defined in my app:

app.router.get('/force',function() {
...
});

But when I make this API call via curl, it fails with HTTP status code 501.

curl -i -X GET "http://localhost/force?page=1"

HTTP/1.1 501 Not Implemented
x-powered-by: flatiron 0.2.0
server: ecstatic-0.1.6
date: Fri, 31 Aug 2012 04:31:56 GMT
Content-Type: application/json
Connection: keep-alive
Transfer-Encoding: chunked

How do I prevent that 501 from occurring?

By the way, I noticed that the request is working with the query and parsing it into the this.req.query object:

...
  method: 'GET',
  url: '/force?page=1',
  query: { page: '1' },
  httpVersion: '1.1',
...

Pavan Kumar Sunkara

unread,
Aug 31, 2012, 1:36:41 AM8/31/12
to flati...@googlegroups.com
Hey,

The following should work

app.router.get(/\/force\??.*/,function() {
...
});
--
- Pavan Kumar Sunkara

Garrett Wilkin

unread,
Aug 31, 2012, 12:52:25 PM8/31/12
to flati...@googlegroups.com
Hi Pavan,

Thanks for the quick reply!  I guess that the answer is that I should be using the regex functionality that comes with director.  I wonder if handling querystrings are a common enough use case for URL routing that an example might be warranted in the docs?  It seems like a good use case for the regex routing.

I tried using what you pasted, and it didn't quite work.  After trying a few random mutations of that regex string, I'm think I'm going to look up regex and try to write my own.  

Btw, it seems like since I'm using the actual flatiron module itself, I need to wrap the path expression in quotes.  I guess that if you use the director module directly, the single quotes aren't necessary?

If I get it working I'll post back here.

Is there anyway that i can see what's going on as director is attempting to match the URL with the defined routes?  Like a debugging output?

Garrett Wilkin

unread,
Aug 31, 2012, 1:31:19 PM8/31/12
to flati...@googlegroups.com
Is the best way to handle querystrings to use just use the simple route definition:

app.router.get('/force',function() {

and then work with this.req.query?

the only complication I see there is detecting whether or not this.req.query is an empty object.  I'm getting good google results for checking for empty objects though, so I think I should be able to hop over that hurdle.

As an aside, i did a bunch of experimenting with the regex path approach by changing the regex, and then printing out app.router.routes to console, like this:

console.log(app.router.routes);

which prints:

{ audit: { get: [Function], post: [Function] },
  force: { get: [Function] },
  pulse: { get: [Function], post: [Function] },
  shlocks: { get: [Function] } }

Of course it looks crazier when you are using Regex.
Reply all
Reply to author
Forward
0 new messages