Skip to content
Domingo Ernesto Savoretti edited this page Apr 1, 2014 · 26 revisions

Non routing-related methods and properties

Listing, in no particular order and by no means complete of the support tools that NSR has to offer

Stand-alone utilities

  • mk-server: tiny tool that constructs a web server (server. coffee, or server.js if invoked adding js argument, like so: mk-server js; stay tuned though, because the order of precedence, i.e. making js the default may change upon feedback received). It's also planned to add it the capability of generating a full web app, not just a server. If you install NSR globally this tool will be available system wide.

router.utils

  • router.utils.mime_types: a dictionary of mime types, initially devised to be used in NSR inner workings, it became - as happened to other constituents of NSR, you'll see more of this - a handy element by itself.

  • router.utils.uuid: unique identifier returning utility, initially devised to be used for cookie related activities, specifically session handling, may be invoked like so: var uid = router.utils.uuid(); // '1644197d-97ae-4ba1-e6bc-18200f00fb31'

  • router.utils.isEmptyObj: as the name implies, may be used to determine if a given object lacks 'own' properties, for example:

     router.utils.isEmptyObj({})  // true

     router.utils.isEmptyObj({name: 'Joe'}) // false 
  • router.utils.extendObj(destObj, srcObj): method that extends the destination object (first argument) with the properties of the source object (second argument), overwriting those that are present in both object and adding those that are lacking in the destination object, for example:
    var srcObj = {age: 60, titles: 34};
    var destObj = {name: 'Joe', age: 59};
    router.utils.extendObj(destObj, srcObj); // returns {name: 'Joe', age: 60, titles: 34} which is the value now destObj has.
  • router.utils.cookie2obj(cookieString) and router.utils.obj2cookie(obj): mostly self explanatory, like this:
    var cookie = "name=Joe; age=60; titles=34";
    var obj = router.utils.cookie2obj(cookie); // returns {name: "Joe", age: 60, titles: 34}
    obj.city = 'Rosario';
    var newCookie = router.utils.obj2cookie(obj); // returns "name=Joe; age=60; titles=34; city=Rosario"
  • router.getCookie(request, cookie_name): retrieves the current request.headers.cookie as a javascript object, unless the argument cookie_name is provided, in which case the object returned has only one property, this is cookie_name if it is present in the cookie, or an empty object if not. For example:
    //The original cookie: "preferredColor=red; nsr_id=1644197d-97ae-4ba1-e6bc-18200f00fb31"
    router.getCookie(request); // returns {preferredColor: "red", nsr_id: "1644197d-97ae-4ba1-e6bc-18200f00fb31"}
    router.getCookie(request, "nsr_id"); // returns {nsr_id: "1644197d-97ae-4ba1-e6bc-18200f00fb31"}
    router.getCookie(request, "times_here"); // returns {} 
  • control flow (async programming): this issue deserves a chapter by itself (or rather, a book), see Async
Clone this wiki locally