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

Node.js+;基于Express的应用程序未使用iisnode运行,node.js,iis,express,iisnode,Node.js,Iis,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.lis

我正在用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);
});
// 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);
package.json:

{
  "name": "TestExpress",
  "version": "0.0.0",
  "private": true,
  "dependencies": {
    "express": "3.x"
  }
}
web.config:

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>
  </system.webServer>
</configuration>
<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>
<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>

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

$(文档).ready(函数(){
console.log(“准备就绪!”);
$(“#你好”).html(“你好,世界!”);
});
我的配置是:windows8.1,iis8;iisnode版本0.2.11,节点版本v0.10.28

从命令行(
C:\tsapp deploy\tsappsvr\TestExpress\0.0.0>node app.js
)启动时,应用程序按预期运行:在浏览器中,我转到
http://localhost:2709/
并查看“你好,世界!”

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

我想问题可能出在我的web.config中,但我不知道如何更改它以使我的应用程序正常工作。我已经尝试了对web.config的一些更改,正如在对类似问题的其他回答中所建议的那样,但还没有成功。 有什么建议吗


提前谢谢。

埃里克,你安装了吗

以下是我在web.config中的配置:

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>
  </system.webServer>
</configuration>
<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>
<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>

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

  • 首先,我安装了url重写扩展,再次感谢,David
  • 其次,我更改了我的web.config,如下所示:
web.config:

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>
  </system.webServer>
</configuration>
<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>
<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>
变量
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
附加到前一个URL中,我将启动REST api处理程序。
http://localhost:2709/
作为基本URL。

谢谢David,我没有安装URL重写扩展。现在我有了它,我将重写规则添加到by web.config中,但还没有成功。我只成功地得到了如下更改app.js的响应:
var express=require('express');var http=require('http');var-app=express();使用(函数(req,res,next){res.send('Hello,World!');});var server=http.createServer(app);var port=process.env.port | | 2709;监听(端口)
,但当我尝试类似于
app.use(“/static”,express.static(uu dirname+'/public'))的东西时我的应用程序一直处于“包罗万象”中间件中。添加deployPath为我解决了一些问题。谢谢我在提供public/javascripts或public/stylesheets目录中相应的静态内容时遇到问题。我正在使用app.use(express.static(path.join(uu dirname,deployPath,'public'));在我的express app.js文件中,我找到了它:app.use(deployPath,express.static(path.join(uu dirname,'public'));