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

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

问题描述

我在 IIS 中有以下设置:

I have the following setup in IIS:

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

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

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.

经过大量时间的浪费和挣扎,我能够将所有部分放在一起,让我的应用程序在虚拟目录中运行,其中包括使用 Socket.io 的应用程序

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

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

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.

我强烈建议使用下面的 Web.config 模板来完成这项工作.

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

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

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.

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

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

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

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.

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

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.

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

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

这会从我们的 Web.config 中检索我们的 virtualDirPath,并给它一个空字符串的默认值.

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

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

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

如果您使用的是 Bower.io,也是如此:

Same thing if you are using Bower.io:

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

通过 Express & 使用虚拟目录Socket.io

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

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.

我们需要配置我们的 Socket.io 服务器,与您通常的配置稍有不同.

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

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

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).

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

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" />

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

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>

客户端

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

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

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

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

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

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天全站免登陆