Node.js 将Express 4.x应用程序部署到Windows Azure

Node.js 将Express 4.x应用程序部署到Windows Azure,node.js,azure,Node.js,Azure,新版本的Express将模块与“服务器”文件分离。它现在生活在/bin/www中,如果可能的话,我更愿意保持这个惯例 在package.json文件中,“开始”脚本显然指向了正确的位置,但Azure似乎忽略了这一点 如何在根目录中没有server.js文件的情况下部署Express 4.x应用程序?我需要做的就是让它自动调用node/bin/www,而不是nodeserver.js。我是否可以为云主机(Azure)添加另一个根配置文件?这就是我如何在Heroku中工作的原因,等等。更新答案 Az

新版本的Express将模块与“服务器”文件分离。它现在生活在
/bin/www
中,如果可能的话,我更愿意保持这个惯例

package.json
文件中,“开始”脚本显然指向了正确的位置,但Azure似乎忽略了这一点

如何在根目录中没有
server.js
文件的情况下部署Express 4.x应用程序?我需要做的就是让它自动调用node
/bin/www
,而不是node
server.js
。我是否可以为云主机(Azure)添加另一个根配置文件?这就是我如何在Heroku中工作的原因,等等。

更新答案 Azure团队已经在内部解决了这个问题。新部署的express 4应用程序在Azure网站上应该可以正常运行,无需任何其他更改

原始答案 我将从tl开始;dr.在应用程序的根目录中创建一个web.config文件,并使用此xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>

    <!-- 
      By default IIS will block requests going to the bin directory for security reasons. 
      We need to disable this since that's where Express has put the application entry point. 
    -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin" />
        </hiddenSegments>
      </requestFiltering>
    </security>

    <handlers>
      <!-- Indicates that the www file is a node.js entry point -->
      <add name="iisnode" path="/bin/www" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^bin\/www\/debug[\/]?" />
        </rule>

        <!-- 
          First we consider whether the incoming URL matches a physical file in the /public folder. 
          This means IIS will handle your static resources, and you don't have to use express.static 
        -->
        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}"/>
        </rule>

        <!-- All other URLs are mapped to the node.js entry point -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="/bin/www"/>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

这有点乱,需要一段时间才能找到。我真的希望它足够聪明,可以查看package.json,但这是我们现在必须处理的问题。通常,Azure通过检查app.js或server.js文件来确定它是否是节点应用程序。如果找到该文件,它将自动创建一个与上面非常类似的web.config文件。在本例中,它将检测app.js,但与99%的其他节点应用程序不同,这实际上不是入口点。我们需要做的是将入口点更改为/bin/www,如上图所示


我们遇到的另一个问题是,出于安全原因,默认情况下IIS会阻止对bin文件夹的请求。我们可以重命名express bin文件夹,也可以告诉IIS放弃它。这就是xml文件的HiddenSegates部分的用途。

Timothy我刚刚测试了它,它工作得完美无缺。感谢您提供有关Azure(和IIS)如何处理请求的答案和见解。我无法在
服务器.js
中获得唯一依赖项为
“express”:“^4.10.7”
的应用程序,而不会出现500服务器错误。Azure是否仍然修复了这一问题?该示例仅侦听端口3000。您需要将其更改为在process.env.PORT上侦听
var server=app.listen(process.env.PORT | | | 3000,函数(){
应该可以工作。