【问题标题】:How to run a node.js app in an azure website subdirectory如何在 azure 网站子目录中运行 node.js 应用程序
【发布时间】:2015-02-15 18:22:17
【问题描述】:

我已经使用 Azure 创建了一个网站,我想在子目录(例如 app1/test)中运行 node.js 应用程序,而不是创建一个新网站并在根目录中运行该应用程序。

这是我的目录结构

-wwwroot/web.config

-wwwroot/index.html

-wwwroot/app1/test/package.json

-wwwroot/app1/test/server.js

所以根目录中有一个 index.html,然后我在 test 子目录中有 server.js。

这是我的 server.js:

var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(port);

这是我的 package.json:

{
  "name": "test",
  "version": "0.0.0",
  "description": "test",
  "main": "server.js",
  "author": {
    "name": "name",
    "email": ""
  },
  "dependencies": {
  }
}

我的 web.config:

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <location path="app1/test">
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="false" />
    <!-- indicates that the server.js file is a node.js application 
    to be handled by the iisnode module -->
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="app" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
            <match url="iisnode.+" negate="true" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            <action type="Rewrite" url="server.js" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>  
  </location>
    <system.webServer>
     <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>
    </system.webServer>
  </configuration>

但我明白了

“由于发生内部服务器错误,无法显示页面。”

当我点击域名/app1/test时

不确定我的 web.config 出了什么问题,以及是否需要手动安装 iisnode。

感谢您的帮助。

谢谢

【问题讨论】:

    标签: javascript asp.net node.js azure iisnode


    【解决方案1】:

    您需要检查您的请求网址是否在您正在检查的位置,在这种情况下:

    var http = require('http');
    var port = process.env.port || 1337;
    http.createServer(function (req, res) {
      if(req.url === '/app1/test')
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end('Hello World\n');
    }).listen(port);
    

    然后你可以去页面测试一下

    http://localhost:1337/app1/test
    

    【讨论】:

    • 我得到相同的结果:(
    • 我刚刚编辑了我的答案以提供更多细节。我测试了它,它可以工作
    • 还是一样。我的 server.js 在 locahost 中工作,但在 azure 中不行。我认为问题不是 server.js 代码。问题似乎是 Web.config。跨度>
    【解决方案2】:

    我解决了!以下作品!

    <match url="app1/test" />
    <action type="Rewrite" url="app1/test/server.js" />
    

    【讨论】:

      猜你喜欢
      • 2023-01-11
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      相关资源
      最近更新 更多