Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an iisnode-hosted web application work out the virtual path at which it is hosted?

I am using iisnode to host a node web application in IIS on Windows. When I set up my routes on the web server (say Express or Restify) I want do do it in a way that is independent of the path that IIS is hosting the application at.

For example, my application could be hosted at two different paths on two machines running IIS; the first could host it at /foo and the second at /bar. Say my application wants to set up a route at /baz - on the first machine I would need to configure the route at /foo/baz and on the second I would have to use /bar/baz which means duplicating some of the configuration held in IIS.

Is it possible to reliably work out the path from within an application hosted with iisnode so that I can calculate the correct routes to set up? Or is there another technique I can use to avoid needing to do so?

like image 869
GraemeF Avatar asked May 10 '12 18:05

GraemeF


People also ask

Where can I find IIS virtual directory?

If you are using Windows 8 or Windows 8.1: Hold down the Windows key, press the letter X, and then click Control Panel. Click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.

Is difference between application and virtual directory?

A virtual directory is just a pointer to a different location on disk. It points to a fixed path on disk that is different that its parent folder. An application is a boundary between different folders. ASP.NET uses it to create a new AppDomain root (or application root).

Why do we use virtual directory in IIS?

For example, you might use a virtual directory when you want your application to include images from another location in the file system, but you do not want to move the image files into the physical directory that is mapped to the application's root virtual directory. By default, IIS uses configuration from Web.


2 Answers

I am not aware of a way to avoid seeing the path segment corresponding to the virtual directory within your node.js application hosted in iisnode. The best way to ensure your application code is host agnostic is to host it at the root of an IIS web site.

like image 122
Tomasz Janczuk Avatar answered Oct 05 '22 11:10

Tomasz Janczuk


@GraemeF: This is a little hacky, but this is who I got around this limitation.

First, I added a global variable to my app called home:

var express = require('express'),
    app = express.createServer(),
    home = process.env.deployPath || '';

Then, I set up my routes using the home variable:

app.get(home + '/baz', function(req, res) {
    res.send('This is the /baz route');
    res.end();
});

Then, it's just a matter of setting the deployPath appSetting in your apps web.config:

Machine 1:

<appSettings>
    <add key="deployPath" value="/foo" />
</appSettings>

Machine 2:

<appSettings>
    <add key="deployPath" value="/bar" />
</appSettings>

You can manually set the value on each machine, have your build process set the value, or even add the setting to the target machine's global web.config.

like image 31
Jason Avatar answered Oct 05 '22 13:10

Jason