Skip to content

Instantly share code, notes, and snippets.

@tjanczuk
Created May 4, 2012 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjanczuk/2598211 to your computer and use it in GitHub Desktop.
Save tjanczuk/2598211 to your computer and use it in GitHub Desktop.
iisnode interceptor
(function () {
// refactor process.argv to determine the app entry point and remove the interceptor
var appFile;
var newArgs = [];
process.argv.forEach(function (item, index) {
if (index === 2)
appFile = item;
if (index !== 1)
newArgs.push(item);
});
process.argv = newArgs;
// intercept console.log to convert strings to uppercase
var oldLog = console.log;
console.log = function (thing) {
if (typeof thing === 'string')
oldLog(thing.toUpperCase());
else
oldLog.apply(this, arguments);
};
// run the original application entry point
require(appFile);
})();
nodeProcessCommandLine: "c:\program files (x86)\nodejs\node.exe" "c:\program files\iisnode\www\helloworld\interceptor.js"
@tjanczuk
Copy link
Author

tjanczuk commented May 4, 2012

The scenario is to intercept invocations to console.* functions in node.js apps run under iisnode to redirect (or fork) the output to custom locations (e.g. specific files int the file system, cloud storage, web socket connection).

The design is following:

  • Use the nodeProcessCommandLine iisnode configuration property to specify that iisnode should always run a predefined interceptor.js file instead of the actual application entry point; note that iisnode will still append the fully qualified application entry point file name as a command line parameter to nodeProcessCommandLine.
  • Within interceptor.js, we intercept the console.* APIs we care about before running the original application entry point code. This means the application code will execute against the rigged version of console.* APIs, since native modules are cached globally by default.
  • interceptor.js also fixes up the process.argv to remove itself from the list to ensure backwards compat with the edge case of iisnode hosted applications that rely on the order of parameters passed in.

@williamkapke
Copy link

The interceptor should overwrite require.main.filename to point to the one that is expected. It is used to figure out what module kicked off a process. In a web app, it is often used to find the root of the website.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment