Created
May 4, 2012 22:55
-
-
Save tjanczuk/2598211 to your computer and use it in GitHub Desktop.
iisnode interceptor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nodeProcessCommandLine: "c:\program files (x86)\nodejs\node.exe" "c:\program files\iisnode\www\helloworld\interceptor.js" |
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
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:
nodeProcessCommandLine
iisnode configuration property to specify that iisnode should always run a predefinedinterceptor.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 tonodeProcessCommandLine
.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 theprocess.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.