0

I would like to write non-trivial dynamic clean URLs in node.js. For example

mynodeserver.com/browse/itemid 

instead of

mynodeserver.com/browse?id=itemid

Right now my static clean urls (browse,login,register,etc) are based on string comparison, which obviously is not the complete solution for this problem.

var pathname = url.parse(request.url).pathname;
//check if there is a request handler for this path
if (typeof handle[ pathname ] === 'function') 
{
    handle[pathname]( response, request, postData );
} 

Is there a native way (or light plugin) to write non-trivial clean URLs?

I'm using Cloud9 IDE

supertopi
  • 3,469
  • 26
  • 38

1 Answers1

2

Writing your own parser for urls should not be a difficult job I think. Assuming you know how to use regular expressions.

I've never heard about standalone plugin for doing this, but you can use very good Express framework. It has url parser built-in.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • you were right, I was able to pull it off with some RegExp magic and tricks. – supertopi Mar 13 '12 at 16:54
  • @supertopi can you provide the code that you used to get the clean urls working?? Is is possible to have two levels of path like xyz.com/tag/football/1, xyz.com/tag/cricket/2 where cricket and football are tag names and 1 and 2 are page numbers. – peter Apr 24 '13 at 12:44
  • @peter hey, it's been a long time since I wrote this parser. Here's some of the code, you may or may not find it useful. http://pastebin.com/RTUMt0cr – supertopi May 08 '13 at 09:08