基于Node.js + Express的应用程序未运行iisnode

eca*_*eca 8 iis node.js express iisnode

我正在使用node.js + express和iisnode进行一些实验.我有以下非常简单的应用程序,位于C:\tsapp-deploy\tsappsvr\TestExpress\0.0.0:

app.js:

var express = require('express');
var app = express();
app.use(express.static(__dirname + "/public"));

var port = process.env.PORT || 2709;
app.listen(port, function() {
  console.log('Listening on port ' + port);
});
Run Code Online (Sandbox Code Playgroud)

的package.json:

{
  "name": "TestExpress",
  "version": "0.0.0",
  "private": true,
  "dependencies": {
    "express": "3.x"
  }
}
Run Code Online (Sandbox Code Playgroud)

web.config中:

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

公共/ index.html的:

<!doctype html>
<html>
<head>
  <script language="javascript" type="text/javascript" src="js/jquery.js"></script>
  <script language="javascript" type="text/javascript">
    $(document).ready(function() {
      console.log("READY!");
      $("#hello").html("Hello, World!");
    });
  </script>
</head>
<body>
  <h1 id="hello" style="text-align:center;"></h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的配置是:Windows 8.1,IIS 8; iisnode版本0.2.11,节点版本v0.10.28.

当从命令行(C:\tsapp-deploy\tsappsvr\TestExpress\0.0.0>node app.js)启动时,应用程序按预期运行:在浏览器中,我转到http://localhost:2709/并看到"Hello,World!".

我的IIS当前正在运行其他基于node.js的应用程序,不使用来自类似位置的快递(即来自C:\tsapp-deploy\tsappsvr\appname\x.y.z\index.js),所以我认为它应该正确配置,但是当我尝试从浏览器运行此应用程序时,键入http://localhost/tsappsvr/TestExpress/0.0.0/app.js我得到404不在Chrome和Firefox中找到(在IE中)或"无法获取/tsappsvr/TestExpress/0.0.0/app.js".

我想这个问题可能在我的web.config中,但我无法弄清楚如何更改它以使我的应用程序正常工作.我已经尝试了对web.config的一些更改,如其他类似问题的答案所示,但尚未成功.有什么建议?

提前致谢.

eca*_*eca 15

我想我找到了解决问题的方法:

  • 首先我安装了url-rewrite扩展,再次感谢David.
  • 其次我改变了我的web.config如下:

web.config中:

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="myapp">
          <match url="/*" />
          <action type="Rewrite" url="app.js" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
  <appSettings>
    <add key="deployPath" value="/tsappsvr/TestExpress/0.0.0" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

请注意deployPath键中的appSettings部分,其值设置为应用程序的虚拟路径(在IIS中设置).就我而言/tsappsvr/TestExpress/0.0.0.

  • 最后,我的app.js现在如下:

app.js:

// Preamble, so to say
var express = require('express');
var http = require('http');
var app = express();

// Variable deployPath is set in web.config and must match
// the path of app.js in virtual directory.
// If app.js is run from command line:
//   "C:/tssapp-deploy/tsappsvr/TestExpress/0.0.0> node app.js"
// deployPath is set to empty string.
var deployPath = process.env.deployPath || "";

// Static content server
app.use(deployPath + "/pages", express.static(__dirname + '/public'));

// REST API handler (placeholder)
app.all(deployPath + "/api", function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end("<h2>REST API Handler found.</h2>");
});

// Default handler
app.get(deployPath + "/", function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end("<h2>Default Handler found.</h2>");
});

// Not Found handler
app.use(function(req, res, next){
  res.writeHead(404, {'Content-Type': 'text/html'});
  res.write("<h2>The requested resource is not available.</h2>");
  res.write("Request received on port " + process.env.PORT + "<br>");
  res.write("Request URL: " + req.url + "<br>");
  res.write("Deploy Path: " + deployPath + "<br>");
  res.end('[iisnode version is ' + process.env.IISNODE_VERSION + ', node version is ' + process.version + ']');
});

// Server creation
var server = http.createServer(app);
var port = process.env.PORT || 2709;
server.listen(port);
Run Code Online (Sandbox Code Playgroud)

变量deployPath用作所有处理程序的前缀("未找到"处理程序除外).当从iisnode启动app.js时,deployPath是其中的一部分process.env,而如果app.js是从命令行启动的,则它是未定义的(例如C:/tsapp-deploy/tsappsvr/TestExpress/0.0.0>node app.js).这可以确保app.js在两种情况下都能正常工作.

现在,http://localhost/tsappsvr/TestExpress/0.0.0/在浏览器中输入我激活默认处理程序; 附加/pages到以前的URL我可以访问静态内容,同时附加/api我激活REST API处理程序.同样的情况,对于http://localhost:2709/作为基本URL.


Dav*_*ias 4

Eric,你安装了url-rewrite 扩展吗?

这是我的 web.config 中的配置:

<configuration>
 <system.webServer>

  <handlers>
   <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
  </handlers>

  <rewrite>
    <rules>
      <rule name="myapp">
        <match url="/*" />
        <action type="Rewrite" url="app.js" />
      </rule>
    </rules>
  </rewrite>
 <system.webServer>    
<configuration>
Run Code Online (Sandbox Code Playgroud)