0

Background:

  • Nodejs app using expressjs.
  • Hosted on IIS using IISNode
  • Nodejs app is in virtual directory called /myVirtualDirectory

Problem:

You want to provide static files or css using less but the url that is passed to nodejs is the full url and doesn't match what would be expected with a standalone nodejs app.

Derek Ekins
  • 11,215
  • 6
  • 61
  • 71

2 Answers2

2

Solution:

var express = require('express');
var app = express();
var lessMiddleware = require('less-middleware');


app.use('/myVirtualDirectory', lessMiddleware({
    src: __dirname + '/public',
    compress: true
}));

app.use('/myVirtualDirectory', express.static(__dirname + '/public'));

Note where we have specified the middleware to use we have passed in the url prefix for it to respond to. As long as this is the same as the name of the virtual directory this will match and your files will be served up as expected.

Derek Ekins
  • 11,215
  • 6
  • 61
  • 71
2

One of the benefits of hosting node.js apps in IIS using iisnode is that you can rely on the static file handler in IIS to serve your static files. The benefit is a substantial improvement in performance, since requests for static content are served by native code without ever invoking JavaScript.

To set up a node.js application hosted in IIS using iisnode to serve static files using IIS static file handler, use the URL rewriting module as described in http://tomasz.janczuk.org/2012/05/yaml-configuration-support-in-iisnode.html

To understand the performance benefits of using static file handler instead of node.js modules to serve static files, read http://tomasz.janczuk.org/2012/06/performance-of-hosting-nodejs.html.

Tomasz Janczuk
  • 3,220
  • 21
  • 19