简体   繁体   English

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

[英]Deploying Express 4.x app to Windows Azure

The new version of Express separates the modules from the "server" file. 新版本的Express将模块与“服务器”文件分开。 It now lives in /bin/www and I'd prefer to keep this convention if possible. 它现在位于/bin/www ,如果可能,我更愿意保留此约定。

In the package.json file, the "start" script clearly points to the right place, but this is seemingly ignored by Azure. package.json文件中,“start”脚本明确指向正确的位置,但Azure似乎忽略了这一点。

How do I deploy an Express 4.x app without having a server.js file in the root directory? 如何在根目录中没有server.js文件的情况下部署Express 4.x应用程序? All I need to do is make it automatically call node ./bin/www instead of node server.js . 我需要做的就是让它自动调用节点./bin/www而不是节点server.js Is there another root configuration file I can add specific to the cloud host (Azure?) This is how I got this working in Heroku, etc. 是否有另一个根配置文件,我可以添加特定于云主机(Azure?)这是我如何在Heroku等工作。

updated answer 更新的答案

The Azure team has since fixed this internally. Azure团队已经在内部修复了这个问题。 A newly deployed express 4 app should work just fine on Azure Websites without any additional changes. 新部署的express 4应用程序应该可以在Azure网站上正常运行而无需任何其他更改。

original answer 原始答案

I'll start with the tl;dr. 我将从tl开始;博士。 Create a web.config file in the root of your application and use this xml. 在应用程序的根目录中创建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>

This is a bit of a mess and took a while to track down. 这有点乱,需要一段时间才能追踪。 I really wish it was smart enough to look at package.json, but this is what we have to deal with for now. 我真的希望看看package.json是否足够聪明,但这是我们现在要处理的问题。 Normally Azure determine if it is a Node application by checking for an app.js or server.js file. 通常,Azure通过检查app.js或server.js文件来确定它是否是Node应用程序。 If it finds that file, it will automatically create a web.config file very similar to what is above. 如果找到该文件,它将自动创建一个非常类似于上面的web.config文件。 In this case, it will detect app.js, but unlike 99% of other node applications, that's not actually the entry point. 在这种情况下,它将检测app.js,但与99%的其他节点应用程序不同,这实际上不是入口点。 What we have to do is change the entry point to /bin/www like shown above. 我们要做的是将入口点更改为/ bin / www,如上所示。

The other issue we run into is that by default IIS blocks requests to the bin folder for security reasons. 我们遇到的另一个问题是,出于安全原因,IIS默认阻止对bin文件夹的请求。 We can either rename the express bin folder, or tell IIS to get over it. 我们可以重命名快递bin文件夹,或告诉IIS克服它。 That's what the hiddenSegments part of the xml file is for. 这就是xml文件的hiddenSegments部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM