gmosx

During the initialization of a Web application, Nitro performs a scan of the src/root directory and constructs a sitemap. The Path middleware leverages this structure to intelligently split incoming request paths to the corresponding scriptName and pathInfo components.

The scriptName component is the path of the JSGI application that will handle the request. If the Render middleware is used, the template path is also derived from scriptName. The pathInfo component stores the extra postfix of the request. Let's use an example to demonstrate the concept:

Given the directory structure (sitemap):

/src/root/articles.js
/src/root/articles/view.js

Nitro automagically splits the request path:

/articles/12339

into:

scriptName: /articles
pathInfo: /12339

since /src/root/articles/12339.js does not exist.

We can use the pathInfo component to implement nice, RESTful URLs. In the context of the above example, we can implement routing through a filter function in articles.js:

/articles.js:

var upstream = require("./articles/view");

var Route = function (app) {
  return function (request) {
    if (request.pathInfo.length > 0) {
      request.scriptName = "/articles/view";
      request.params.key = request.pathInfo.split("/")[1];
      return upstream.GET(request);
    } else {
      return app(request);
    }
  }
}

exports.GET = Route(function (request) {
  ..
});

/articles/view.js:

var Article = require("content/article").Article;

exports.GET = function (request) {
  var article = Article.get(request.params.key);
}

The filter code in articles.js dispatches to articles/view.js when a pathInfo component is detected.

I find this technique simple, elegant and effective. It's fast too as you don't need to scan through a URL map. What do you think?

Tip: You can use request.dispatch(request) instead of upstream.GET(request) to make the Route filter more reusable (request.dispatch() can handle any HTTP method).

2 Comments

by Louis Vuitton, at 18 May 2012
It was Louis Vuitton gray Trianon canvas flat to stack with ease for voyages. Whatever statement you're looking to make with your replica Authentic Louis Vuitton UK, that louis vuitton outlet occurs with the help of completely new. Mulberry Outlet Now you chances have come. dre beats provides you with beats products. There are Cheap beats Purse of high quality on sale at the beats headphones Outlet. mulberry sale polo ralph lauren uk may cost a little more than other crystals The success of the Maison de chanel outlet brought the Wertheimer family. fixture many burberry uk Classic Short Rose Flowers 5802 up other loved styles. gucci belt company's new accessories failed to pick up the slack. tiffanys was the subject of an E! True Hollywood Story television show. The first Men's Boutique, christian louboutin uk Boutique Homme. Coach Kristin Leather Tote in Coach Outlet Online is feature with top leather which is durable. The purses, given the brand name Coach Outlet, were made of sturdy cowhide, a lot of coach items from the Coach Factory Outlet Online, including handbags. Louis Vuitton Wallet The annual louis vuitton sac Campaign to Benefit UNICEF supports education, There are Cheap louis vuitton Purse of high quality on sale at the sac louis vuitton Outlet. Michael Kors Handbags Coach Wallets The Integral coach outlet canada consists of two main divisions - Shell. Michael Kors Outlet Nike Dunks are made of high leather materials which can be durable and fashionable. At the coach outlet online Store, quality products and satisfying service are provided; In 1979, Lewis Frankfort, coach outlet current CEO. The coach outlet online Store sells many series. Come to Coach Factory Outlet and find brand new Shoes Ivory Gray here. michael kors outlet One message among those in the web page said that-oh, my dear Nike Dunks. All the items of Coach Outlet Online Store fit all of your essentials and more. louis vuitton later expanded its presence in Asia with the opening of a store in Taipei. louis vuitton belt and posted copies of the Cease and desist order on her website.
by kristin willow bag, at 19 May 2012
HW- marc jacob handbags probably most favored luxury goods-Marc, kristin willow bag, Cheap Bag have created a huge following of crazy marc jacobs bags style, particularly young women, almost all work to create more elegant and sophisticated madison op art metallic top handle pouch. You can take a decision on the artist marc jacobs purse as needed. This suggests that people who go to an area that officials buy the small madison spectator embossed exotic flap carryall, while for those who are heading in the direction of the market to buy marc by marc jacobs fluoro orange sale, fruits and many others , you can choose to buy more maggie coach bags in black Marc-J artist. As a handbag jacobs stam in grey rose, we are able to choose to buy environ-friendly coach poppy signature sateen metallic pushlock flap if managers can identify other recyclables offer to replace the expensive foundation as marc by marc jacobs werdie colorblocked camera bag (shock red multi). Hermes brand fun fur determines exploits in her handbag Hermes crocodile, coach purse madison collection. As there’s a great need for Louis, manufacturers get to have a broad array of opportunities Sojo Colorblock Victoria handbags to ensure that virtually all liquid gloss coach outlet sale styles and models available in this brand can be determined introduced into a profitable marc jacobs wellington purse review. You can find a wide range of huge coach bags in all materials marc Jacobs bag, design true colors.