Skip to content

Instantly share code, notes, and snippets.

@Junch
Forked from burmisov/iisnode-baseurl.js
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save Junch/0f218f8aef44e34727b0 to your computer and use it in GitHub Desktop.

Select an option

Save Junch/0f218f8aef44e34727b0 to your computer and use it in GitHub Desktop.
Cut off IIS virtual path (base url) from the request url when running with IISNODE
// Cut off IIS virtual path (base url) from the request url when running via IISNODE
// with URL Rewrite
//
// Add "APPL_MD_PATH" to "promoteServerVars" iisnode attribute in web.config
var iisNodeBaseUrl = function () {
return function (req, res, next) {
var appMdPath = req.headers['x-iisnode-appl_md_path'];
if (appMdPath) {
var rootPos = appMdPath.search("/ROOT");
if (rootPos > -1) {
// e.g. '/LM/W3SVC/1/ROOT/testbaseurl' -> '/testbaseurl'
var baseUrl = appMdPath.slice(appMdPath.search("/ROOT") + ("/ROOT").length);
if (baseUrl <> "") {
// e.g. '/testbaseurl/assets/js/script.js' -> '/assets/js/script.js'
req.url = req.url.replace(baseUrl, "");
}
}
}
return next();
};
};
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="index.js" verb="*" modules="iisnode" />
</handlers>
<iisnode promoteServerVars="APPL_MD_PATH" />
<rewrite>
<rules>
<rule name="server">
<match url="/*" />
<action type="Rewrite" url="index.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment