Node.js : Connect router middleware

Problem

I’m trying to use Connect for Node.js but having a problem getting the router middleware to do exactly as I want it to.

Specifically, I want to be able to fill the req.params object with the search term portion of a URL, for example

would return req.params = { "q" : "Search" }

I’d prefer to have one setup, where I could do something like :

router.get('/user?:userid&:fullname&:emailaddress', function(req, res) {...

so that I could access check the access for each search term and then respond accordingly.

Is there a way to do this in Connect?

Problem courtesy of: A Wizard Did It

Solution

This is not really answer that will solve your problem using Connect. But I think you should be using Express(by same author TJ) which uses Connect under the covers, but is way more powerful.

Sinatra inspired web development framework for node.js — insanely fast, flexible, and sexy

When using Express achieving this is really easy as shown with app.js:

var express = require('express'),
app = express.createServer();

app.get('/search', function (req, res) {
res.send(req.query);
});

app.listen(3111);

alfred@alfred-laptop:~/node/so/6513101$ curl -v "http://localhost:3111/search?q=a&y=b"
* About to connect() to localhost port 3111 (#0)
* Trying ::1... Connection refused
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 3111 (#0)
> GET /search?q=a&y=b HTTP/1.1
> User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: localhost:3111
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 17
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
* Closing connection #0
{"q":"a","y":"b"}

Solution courtesy of: Alfred

View additional discussion.