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

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

问题描述

我在IIS中进行以下设置:




  • 托管标准HTML网站的默认网站(www.foo.com)

  • 运行IIS节点的默认网站(www.foo.com/bar)下的Web应用程序

  • 节点项目正在利用快递



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



这是我的配置的当前版本: / p>

 < configuration> 
< system.webServer>
<处理程序>
< add name =iisnodepath =app.jsverb =*modules =iisnode/>
< / handlers>
< rewrite>
< rules>
< rule name =bar>
< match url =bar / */>
< action type =Rewriteurl =app.js/>
< / rule>
< / rules>
< / rewrite>
< /system.webServer>
< / configuration>


解决方案

我遇到同样的问题,我的应用程序在虚拟目录中。



经过大量的时间浪费和挣扎,我可以将所有的部分放在一起,让我的应用程序在虚拟目录中工作,这包括使用Socket.io的应用程序



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



我强烈建议您使用下面的Web.config模板来实现。



< h2> IISNode Web.config模板

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



上述链接中的配置有一些我在这里的意见,以便于使用。它被配置为使用app.js作为主文件,但如果您的文件被命名为不同的东西,只需切换该值即可使用该文件。



要使此配置正常工作,您将需要如果您还没有安装IIS,请重新编写IIS的重写模块。



默认设置



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



获取Express使用您的虚拟目录< h2>

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



获取我们的虚拟目录路径



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

  var virtualDirPath = process.env.virtualDirPath || ; 

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

路由页面



然后我们可以将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服务器与您通常会略有不同。

  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.iopath =app.jsverb =*modules =iisnode/> 

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

 < rule name =SocketIOpatternSyntax =ECMAScript> 
< match url =socket.io。+/>
< action type =Rewriteurl =app.js/>
< / rule>



客户端



在客户端 - 我们只需要指定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中运行。另外,通过更改conifg文件中的处理程序,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天全站免登陆