I always get nervous when I see people doing string hacking on URLs.  Do you know about http://nodejs.org/api/url.html ?  Would it help you here?  Maybe this is fine, but fyi for future if not here.