【问题标题】:Node.js + Express based app not running with iisnode基于 Node.js + Express 的应用程序未与 iisnode 一起运行
【发布时间】:2014-06-09 15:12:47
【问题描述】:

我正在用 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);
});

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>

公共/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>

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

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

我的 IIS 目前正在运行其他基于 node.js 的应用程序,而不是使用来自类似位置(即来自 C:\tsapp-deploy\tsappsvr\appname\x.y.z\index.js)的 express,所以我认为它应该正确配置,但是当我尝试从浏览器运行这个应用程序时,输入 http://localhost/tsappsvr/TestExpress/0.0.0/app.js 我在 Chrome 和 Firefox 中得到 404 Not Found(在 IE 中)或“Cannot GET /tsappsvr/TestExpress/0.0.0/app.js”。

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

提前致谢。

【问题讨论】:

    标签: node.js iis express iisnode


    【解决方案1】:

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

    • 首先我安装了 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>
    

    请注意appSettings 部分中的deployPath 键,其值设置为应用程序的虚拟路径(在 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);
    

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

    现在,在浏览器中输入http://localhost/tsappsvr/TestExpress/0.0.0/ 我会触发默认处理程序;将/pages 附加到以前的URL 我可以访问静态内容,而附加/api 我会触发REST API 处理程序。 http://localhost:2709/ 作为基本 URL 也会发生同样的情况。

    【讨论】:

    • 添加 deployPath 为我解决了问题。谢谢!
    • 我在提供 public/javascripts 或 public/stylesheets 目录中的相应静态内容时遇到问题。我正在使用 app.use(express.static(path.join(__dirname, deployPath, 'public')));在我的快速 app.js 文件中
    • 想通了:app.use(deployPath, express.static(path.join(__dirname, 'public')));
    【解决方案2】:

    Eric,你安装了url-rewrite extension 吗?

    这是我在 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>
    

    【讨论】:

    • 谢谢大卫,我没有安装 url-rewrite 扩展。现在我有了它,并通过 web.config 添加了重写规则,但还没有运气。我只成功地响应更改了我的 app.js,如下所示:var express = require('express'); var http = require('http'); var app = express(); app.use(function(req, res, next){ res.send('Hello, World!'); }); var server = http.createServer(app); var port = process.env.PORT || 2709; server.listen(port); ,但是当我尝试类似 app.use("/static", express.static(__dirname + '/public')); 之类的东西时,我的应用程序一直落在包罗万象的中间件中。
    猜你喜欢
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 2023-03-08
    • 2019-08-26
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多