How to enable static files (and less support) when hosting a nodejs app within IIS using IISNode

社会主义新天地 提交于 2019-12-13 21:18:37

问题


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.


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/12307174/how-to-enable-static-files-and-less-support-when-hosting-a-nodejs-app-within-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!