IIS、Node.js 和带有 IISNode 的 Web 应用程序未正确配置虚拟目录 [英] IIS, Node.js, and Web Application with IISNode not configured right with Virtual Directory

查看:28
本文介绍了IIS、Node.js 和带有 IISNode 的 Web 应用程序未正确配置虚拟目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 IIS 中有以下设置:

  • 托管标准 html 站点的默认网站 (www.foo.com)
  • 运行 IIS 节点的默认网站 (www.foo.com/bar) 下的 Web 应用程序
  • Node 项目正在使用 express

我这辈子都无法正确配置这个东西,所以当我点击 Web 应用程序时,它会正确地提供节点应用程序.我认为我的问题在于 web.config.有人能帮我写一个正确的 web.config 来让它正常工作吗?我的配置的当前版本将为我提供一个节点响应,表明它无法在我输入的任何 url 处获取资源.

这是我的配置的当前版本:

<预><代码><配置><system.webServer><处理程序><add name="iisnode" path="app.js" verb="*" modules="iisnode"/></处理程序><重写><规则><规则名称=栏"><match url="bar/*"/><action type="重写" url="app.js"/></规则></规则></rewrite></system.webServer></配置>

解决方案

不久前我遇到了同样的问题,在虚拟目录中运行我的应用程序.

经过大量时间的浪费和努力,我能够将所有部分组合在一起,让我的应用程序在虚拟目录中工作,其中包括使用 Socket.io 的应用程序

由于没有太多针对此特定场景的文档和可用资源,因此我发现仅部分描述了如何解决此问题.这是有关如何使所有这些工作正常工作的教程.我个人有多个 Node.js Web 服务使用此设置实现 REST API 或 Socket.io.

我强烈建议使用下面的 Web.config 模板来使其正常工作.

IISNode Web.config 模板

https://gist.github.com/pbaio/f63918181d8d7f8ee1d2

我在上面链接中的配置中有一些注释以帮助易于使用.它配置为使用 app.js 作为主文件,但如果您的文件命名不同,只需切换值以使用该文件即可.

要使此配置正常工作,您将需要 IIS 的 URL 重写模块如果您还没有安装它.

默认设置

默认情况下,此模板设置为在 IIS 中运行的标准 Web 应用程序中运行,而不是在虚拟目录环境中运行.但是,通过一些细微的调整,您可以使用相同的 Web.config 在虚拟目录中运行 Node.js 应用程序.

让 Express 使用您的虚拟目录

IISNode 使所有在您的 环境变量中声明的键.我们可以利用它来设置我们的虚拟目录路径并将其公开给我们的主文件.在上面的模板中,我们的主文件是 app.js.

获取我们的虚拟目录路径

我们需要在我们的 Web.config 文件中获取我们的应用程序将从中路由的路径.我们通过访问我们的进程对象上的环境变量来做到这一点.将以下行添加到我们的 app.js 文件中.

var virtualDirPath = process.env.virtualDirPath ||'';

这将从我们的 Web.config 中检索我们的 virtualDirPath 并为其提供一个默认值空字符串.

路由页面

然后我们可以将 virtualDirPath 添加到我们的路由中,如果您使用的是视图引擎(例如 Jade 或 EJS),我们可以将超链接等的虚拟目录路径传递给视图:

var app = require('express')();app.get(virtualDirPath + '/', function(req, res) {res.render('index', { virtualDirPath: virtualDirPath });});

静态内容

我们可以轻松地提供如下服务:

app.use(express.static(path.join(virtualDirPath, 'public')));

如果您使用 Bower.io,同样如此:

app.use('/bower_components', express.static(path.join(virtualDirPath,'bower_components')));

在 Express & 中使用虚拟目录Socket.io

在 Socket.io 中使用虚拟目录时,我们需要更改服务器和客户端的配置.

服务器端

我们需要将我们的 Socket.io Server 配置为与您平时的配置略有不同.

var app = require('express')();var virtualDirPath = process.env.virtualDirPath ||'';var server = require('http').Server(app);var io = require('socket.io')(server, { path: virtualDirPath + '/socket.io' });//获取我们应该监听的端口server.listen(process.env.PORT || 8080);

在上面的代码中,我们修改了 Socket.io 服务器以在我们的 virtualDirpath 而不是默认路径上操作('/socket.io' 是默认路径).

Web.config 更改

为了使 IISNode 与 socket.io 正常工作,我们还需要添加一些额外的 url 重写并换出我们的处理程序.在上面的模板配置文件中,我们可以看到第 57 行的 Socket.io 处理程序,它在模板中被注释掉了.

<add name="iisnode-socket.io" path="app.js" verb="*" modules="iisnode"/>

然后我们需要为Socket.io路径添加我们的url重写

<match url="socket.io.+"/><action type="重写" url="app.js"/></规则>

客户端

在客户端,我们只需要指定 Socket.io 服务器正在侦听的路径而不是其默认路径.

var socket = io.connect('http://example.com:port', { path: '/virtualDirPath/socket.io' });

此时,您的 Socket.io 应用程序在使用 IISNode 的虚拟目录中运行,一切都应该很好.

环境信息

使用此配置的应用程序是使用 Node.js、Express 4.12.3 构建的,并在安装了 IISNode 的 IIS 7.5 中运行.此外,通过更改配置文件中的处理程序,Socket.io 也可以在虚拟目录中使用.上面例子中使用的Socket.io版本是1.3.5

I have the following setup in IIS:

  • Default Web Site (www.foo.com) hosting standard html site
  • Web Application underneath Default Web Site (www.foo.com/bar) running IIS Node
  • Node project is utilizing express

I cannot for the life of me get this thing configure correctly so when I hit the web application is serves up the node application correctly. I think my problem lies in the web.config. Can anybody help me write a correct web.config to get this working correctly? The current version of my config will server me a node response that says it cannot get the resource at whatever url I type.

Here is the current version of my config:

<configuration>
  <system.webServer>    
    <handlers>
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>    
    <rewrite>
      <rules>
        <rule name="bar">
          <match url="bar/*" />
          <action type="Rewrite" url="app.js" />
        </rule>
      </rules>
    </rewrite>    
  </system.webServer>
</configuration>

解决方案

I ran into the same issue a while back, running my app in a Virtual Directory.

After lots of time wasted and struggling I was able to put all the pieces together to get my apps to work in a Virtual Directory, this included apps using Socket.io

Since there isn't much documentation out there for this particular scenario and the resources that are available, that I've found, only partially described how to solve this issue. Here is a tutorial on how to get all of this working. I personally have multiple Node.js web services implementing either a REST API or Socket.io using this setup.

I strongly recommend using the Web.config template below to get this working.

IISNode Web.config Template

https://gist.github.com/pbaio/f63918181d8d7f8ee1d2

The config in the above link has some comments I put in there to help with ease of use. Its configured to use app.js as the main file but if your file is named something different simply switch the value to use that file instead.

To get this config working you will need the URL Re-write Module for IIS if you don't already have it installed.

Default Setup

By default this template is setup to work in a standard Web App running in IIS and not in Virtual directory environment. However, with some minor tweaking you can use this same Web.config to run a Node.js app in a Virtual Directory.

Get Express to use your Virtual Directory

IISNode makes all keys declared in your <appSettings> environment variables. We can use this to our advantage to setup our Virtual Directory path and expose it to our main file. In the template above our main file is app.js.

Get our Virtual Directory Path

We need to get the path that our application will be routed from in our Web.config file. We do this by accessing our environment variables on our process object. Add the following line to our app.js file.

var virtualDirPath = process.env.virtualDirPath || '';

This retrieves our virtualDirPath from our Web.config and give it a default value of empty string.

Routing Pages

Then we can prepend the virtualDirPath to our routes and if you are using a view engine such as Jade or EJS we can pass our Virtual Directory path for hyperlinks and such to the view:

var app = require('express')();
app.get(virtualDirPath + '/', function(req, res) {
  res.render('index', { virtualDirPath: virtualDirPath });
});

Static Content

We can serve this up easily as follows:

app.use(express.static(path.join(virtualDirPath, 'public')));

Same thing if you are using Bower.io:

app.use('/bower_components', express.static(path.join(virtualDirPath,'bower_components')));

Using Virtual Directories with Express & Socket.io

When using Virtual Directories with Socket.io we need to make changes to the configuration for both the Server and the Client.

Server-Side

We need to configure our Socket.io Server slightly different than you normally would.

var app = require('express')();

var virtualDirPath = process.env.virtualDirPath || '';

var server = require('http').Server(app);
var io = require('socket.io')(server, { path: virtualDirPath + '/socket.io' });
// Get the port that we should be listening on
server.listen(process.env.PORT || 8080);

In the above code we are modifying our Socket.io server to operate on our virtualDirpath and not the default path ('/socket.io' is the default path).

Web.config changes

In order for IISNode to properly work with socket.io we also need to add some additional url re-writing and swap out our handler. Within the template config file from above we can see the Socket.io handler on line 57, it is commented out in the template.

<add name="iisnode-socket.io" path="app.js" verb="*" modules="iisnode" />

Then we need to add our url re-writing for the Socket.io paths

<rule name="SocketIO" patternSyntax="ECMAScript">
    <match url="socket.io.+" />
    <action type="Rewrite" url="app.js"/>
</rule>

Client-Side

On the Client-Side we just need to specify the path that the Socket.io server is listening at instead of its default path.

var socket = io.connect('http://example.com:port', { path: '/virtualDirPath/socket.io' });

Everything should be good to go at this point with your Socket.io application running in a Virtual Directory with IISNode.

Environment Info

The apps that use this config were built with Node.js, Express 4.12.3 and running in IIS 7.5 with IISNode installed. Additionally, by changing the handler in the conifg file, Socket.io can be used in a Virtual Directory as well. The Socket.io version used in the above example was 1.3.5

这篇关于IIS、Node.js 和带有 IISNode 的 Web 应用程序未正确配置虚拟目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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