• @DrAzzy, cool thing your are up to... #webserver

    you can go even further... as you know, require() can load dynamically - at runtime - module from the card and there is also a way to unload it afterwards.

    Depending on the extension, you first check for a statically loaded function, if it is not there, go for a module, and if there is no module loaded, throw the error... 404.

    What about below code (used long names help with documentation):

    var requestHandler = 
    { handlers: // handlers for extension and file|page (=function) name:
      { json:
        { status: { function() { // handles status.json
            // status function code accepting with 'arguments' - either specified or as array
          }
        , history: function() { // handles history.json
            // history function code ...
          }
        , etc: function() { // handles etc.json
            ...
          }
        , _: function() // default handler for json extension using file(content) as parm
            ...
          }
        } // /json
      , xml:
        { status: function() { // handles status.xml
            // status function code ...
          }
        , history: function() { // handles history.xml
            // history function code ... 
          }
        , etc: function() { // handles etc.xml
            ...
          }
        , _: function() // default handler for xml extension using file(content) as parm
            ...
          }
        } // /xml
      } // handlers
    , statistics: { // houskeeping for to make it really 'smart'/controlled: 
                    // managing handlers like virtual memory managment
                    // or old (non-virtual) drop and reload of exec code.
                    // detect limit on load (see below) and purge 
                    // least used stuff... you can reload later anyway
         ...
      }
    , load: function(extension,functionName,funktion­) { // load a handler function
        var handlers = this.handlers[extension];
        if (!handlers) { handlers = (this.handlers[extension] = {}); }
        if (handlers[functionName]) { console.log("function " + functionName + " already loaded"); }
        handler[functionName] = funktion; // load it anyway?
      }
    , purge: function(extension,functionName) { // purge handler function to free memory
        var handlers = this.handlers[extension];
        if (handlers && handlers[functionName]) { 
          delete handlers[functionName]);
          if (handlers.length === 0) { delete this.handlers[extension]; } }
      }
    , handle: function(path,fileDotExtension,parms,res­ponse) { // request ...
        // request may be url w/ query string and/or body from http request
        // path / uri can be normalized by replacing / with _ and 
        //  become part of the file and thus function name (for now not considered)
        var  fileDotExtensionElements.split("."), file = fdees[0], ext = fdees[1];
        var handlers = this.handlers[ext], funktion;
        if (handlers) { // if you do not purge handler extensions and start with set of 
                        // predefined handler extensions, then you can take this set as 
                        // the criteria to detect invalid / unimplemented extensions...
                        // or put this instead into the stats / housekeeping / control
          var functionName = (file.charAt === "_")  ? "_" : file; // do some defaulting:
            // do default _ function for files beginning w/ _, for example .jpg, etc
          funktion = handlers[functionName]);
          if (!funktion && (funktion = require(functionName + "_"+ ext + ".js")) { 
            // function not in handler but found a module (module name syntax to be defined)
            this.load(ext,functionName,funktion); }
        }
        if (funktion) { // function available, finally do something...
          funktion(parms,....); // ...invoke (or you may also use call or apply)
        } else {
          console.log("no handler for extension " + ext + " and file " + file); 
          // you may want to put also something on the response.
        }
      }  
    }; // /handler
    

    Are you also thinking of a file type / extension that can include references to other files and
    is recursively interpreted (with this handler) to allow page / response composition? ...for example as a nested structure of divs of a streamed out html page?

    The handler as module could also have ways to go after 'load code from EPROM'. Btw, if managed code is eval-ed, it is not THAT a bad thing to do...

    PS: I'm currently in a time/resource bind, otherwise the code above would be tested... ;-)

About

Avatar for allObjects @allObjects started