1

I'm creating an application using Expressjs running on node under IISNode (i.e on windows).

I start by setting things up very much like all the expressjs examples I've seen:

  backend.configure(function() {
    backend.register('html', {
      compile: function(str, options) {
        return function(locals) {
          return str;
        };
      }
    });
    backend.set('views', __dirname + '/views');
    backend.set('view engine', 'html');
    backend.set('view options', {
      layout: false
    });
    backend.use(express.bodyParser());
    backend.use(backend.router);
    backend.use(express.static(__dirname + '/public'));
  });  

Lets say my site is running at localhost://mysite. I'm having to create all my route handlers as follows.

  backend.get('/mysite/index', function(req, res, next) {
    return res.render('index');
  });

i.e. I'm having to prefix them all with "mysite". Non of the examples I've seen require this. Is this something to do with IISNode or something else I haven't configured?

Simon Lomax
  • 8,714
  • 8
  • 42
  • 75

3 Answers3

6

I was looking for this feature but for API routes, not for static files. What I did was that when I initialized the router, I added the mount path. So my configuration looks like this

//Default configuration
app.configure(function(){
    app.use(express.compress());
    app.use(express.logger('dev'));
    app.set('json spaces',0);
    app.use(express.limit('2mb'));
    app.use(express.bodyParser());

    app.use('/api', app.router);        // <---

    app.use(function(err, req, res, callback){
        res.json(err.code, {});
    });
});

Notice the '/api' when calling the router

Diosney
  • 10,520
  • 15
  • 66
  • 111
Xerri
  • 4,916
  • 6
  • 45
  • 54
  • It worth to note that this solution works smoothly with express-resouce. I have several express subapps that provides an API below the prefix /api using this solution and a regular website at /. – Diosney Dec 14 '13 at 14:51
1

Out of the box, this is indeed how you have to do it. You could look into express-resource, enabling resourceful routing - but that comes with it's own caveats, at least when it comes to route-specific middleware.

If it's just one path, I think you can handle that with app.set("basepath", "mysite").

Linus Thiel
  • 38,647
  • 9
  • 109
  • 104
  • 1
    Thanks Linus, I'd didn't have any luck with app.set("basepath", "mysite"). I took a look at express-resource but I also found a related [question](http://stackoverflow.com/questions/4375554/is-it-possible-to-set-a-base-url-for-nodejs-app). In the end I went for [express-namespace](https://github.com/visionmedia/express-namespace). It has [an issue in windows](https://github.com/visionmedia/express-namespace/issues/11) but is easily fixed by amending the index.js file in the express-namespace node_modules folder. – Simon Lomax Feb 27 '12 at 15:21
0

To avoid having to modify your express app when deploying in IIS using iisnode, you need to deploy to the root of the IIS WebSite rather than a virtual directory under a site.

Tomasz Janczuk
  • 3,220
  • 21
  • 19
  • I'm shocked at how many people think that suggesting a needless change to an entire application structure is the solution to this issue. This is the WORST possible answer shy of "it can't be done." – rainabba Nov 02 '13 at 00:40