使用IISNode在IIS中托管Node.js应用程序时如何启用静态文件(和较少的支持) [英] How to enable static files (and less support) when hosting a nodejs app within IIS using IISNode

查看:108
本文介绍了使用IISNode在IIS中托管Node.js应用程序时如何启用静态文件(和较少的支持)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

  • 使用expressjs的Node.js应用.
  • 使用IISNode托管在IIS上
  • Nodejs应用位于名为/myVirtualDirectory的虚拟目录中

问题:

您希望使用更少的文件来提供静态文件或CSS,但是传递给nodejs的URL是完整URL,并且与独立的nodejs应用程序所期望的URL不匹配.

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.

推荐答案

解决方案:

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'));

请注意,在我们指定要使用的中间件的地方,我们已经传递了URL前缀以使其能够响应.只要它与虚拟目录的名称相同,它将匹配并且您的文件将按预期提供.

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.

这篇关于使用IISNode在IIS中托管Node.js应用程序时如何启用静态文件(和较少的支持)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆