简体   繁体   English

如何为MEAN应用程序的Azure部署定义web.config文件?

[英]How to define a web.config file for Azure deployment for a MEAN application?

While my app runs fine locally, I'm having a difficult time deploying it to an Azure Web App. 虽然我的应用程序在本地运行良好,但将其部署到Azure Web App时遇到了困难。

The structure of the application is as follows: 该应用程序的结构如下:

  • ./index.html ./index.html
  • ./app.js - main node.js code, including Express middleware ./app.js-主要的node.js代码,包括Express中间件
  • ./*.js - supporting myapp's node.js code ./*.js-支持myapp的node.js代码
  • ./assets/angular - Angular.js v1.2.x ./assets/angular-Angular.js v1.2.x
  • ./assets/bootstrap ./assets/bootstrap
  • ./assets/css ./资产/ css
  • ./assets/img ./assets/img
  • ./views - views for the application ./views-应用程序的视图
  • state.js routes to /myapp/* (eg, /myapp/home, /myapp/login). state.js路由到/ myapp / *(例如,/ myapp / home,/ myapp / login)。
  • all Express API calls are to /api/* (eg, /api/version) 所有Express API调用都针对/ api / *(例如/ api / version)

After piecing together tidbits from other sources, my best guess for a web.config is as follows: 将其他来源的花絮拼凑在一起后,我对web.config的最佳猜测如下:

<handlers>
  <!-- Indicates that the app.js file is a node.js site to be handled by the iisnode module -->
  <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
  <rules>
    <!-- Do not interfere with requests for node-inspector debugging -->
    <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
      <match url="^app.js\/debug[\/]?" />
    </rule>

    <rule name="Static files" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <!-- Core application files -->
        <add input="{REQUEST_URI}" pattern="assets/" ignoreCase="true" />
        <add input="{REQUEST_URI}" pattern="views/" ignoreCase="true" />
      </conditions>
      <action type="Rewrite" url="{REQUEST_URI}" />
    </rule>

    <rule name="Express.js API">
      <match url="api/*" />
      <action type="Rewrite" url="app.js"/>
    </rule>

    <rule name="Angular">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="index.html" />
    </rule>        
  </rules>
</rewrite>

Unfortunately, after publishing to Azure, while most of the assets/ and index.html file are loaded (as viewed in browser debug window), I get a "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" on the /assets/angular/app.js. 不幸的是,发布到Azure之后,虽然加载了大多数asset /和index.html文件(在浏览器调试窗口中查看),但我收到“加载资源失败:服务器的响应状态为500(内部服务器错误)”上的/assets/angular/app.js。

What could be causing this issue? 是什么导致此问题? Furthermore, is there a standard for deploying MEAN apps on Azure? 此外,是否存在用于在Azure上部署MEAN应用程序的标准? Is there a standard web.config for this type of MEAN app? 是否有针对此类MEAN应用的标准web.config?

The trick for supporting MEAN applications within Azure came down to a few things: 在Azure中支持MEAN应用程序的技巧归结为以下几点:

  • Rewriting the express.js middleware requests to server.js 将express.js中间件请求重写为server.js
  • Rewriting the angular.js URI requests to index.html 将angular.js URI请求重写为index.html
  • Rewriting all static file requests - for example, Angular assets, CSS, Images, etc. - as is/directly to the requested URI 重写所有静态文件请求-例如,Angular资产,CSS,图像等-直接/直接复制到请求的URI
  • Importantly, so that Azure can handle your requests properly through a defined port, opening up the express.js server at process.env.PORT 重要的是,Azure可以通过定义的端口正确处理您的请求,并在process.env.PORT处打开express.js服务器。

Within server.js: 在server.js中:

    // Within server.js - make sure we're listening on the proper port
    server.listen(process.env.PORT, function () {
        logger.info('Web server listening on port ' + process.env.PORT)
    });

Then for web.config: 然后对于web.config:

<handlers>
   <!-- Indicates that the app.js file is a node.js site to be handled by the iisnode module -->
   <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
</handlers>

<rewrite>
  <rules>

  <!-- Do not interfere with requests for node-inspector debugging -->
  <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="^server.js\/debug[\/]?" />
  </rule>

  <!-- For static files, redirect to the URI -->
  <rule name="Static files" stopProcessing="true">
    <match url=".*" />
    <conditions logicalGrouping="MatchAll">
      <!-- Any directory/file in the assets/ or views/ directory -->
      <add input="{REQUEST_URI}" pattern="assets\/" ignoreCase="true" />
      <add input="{REQUEST_URI}" pattern="views\/" ignoreCase="true" />
    </conditions>
    <action type="Rewrite" url="{REQUEST_URI}"/>
  </rule>

  <!-- For Express.js middleware API, if using api/ prefix, then use server.js -->
  <rule name="Express.js URIs">
    <match url="api/*" />
    <action type="Rewrite" url="server.js" />
  </rule>

  <!-- For Angular.js URIs, rewrite to the index.html file -->
  <rule name="Angular">
    <match url=".*" />
    <conditions logicalGrouping="MatchAll">
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="index.html" />
  </rule>  

</rules>
</rewrite>

And for anyone that gets stumped with a similar issue, may I recommend: 对于遇到类似问题的人,我可以建议:

  • Your browser's developer tools - eg, for Chrome - I was able to use console.log's within my Angular application to determine that my express api was called but not returned 您的浏览器开发人员工具(例如Chrome浏览器 )可以在Angular应用程序中使用console.log来确定我的快速api被调用但未返回
  • Postman (to see results of express.js requests), allowed me to view the URI request error codes and determine that the port may be an issue 邮递员 (查看express.js请求的结果),允许我查看URI请求错误代码并确定端口可能是问题
  • Azure's Kudu toolset , which allows access to Application log files, it helped me determine whether it was my application or URI requests to the working application causing the problem Azure的Kudu工具集 ,它允许访问应用程序日志文件,它帮助我确定是我的应用程序还是对工作应用程序的URI请求导致了问题

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

相关问题 如何从web.config文件获取应用程序设置值到angular JS Controller - How to get the app settings value from web.config file to angular JS Controller 任何人都知道如何从我的angularjs中的web.config文件读取配置 - Anyone know how can I read configurations from web.config file in my angularjs AngularJS:如何将数据从 web.config 带到视图 controller? - AngularJS: How to bring a data from the web.config to the view controller? 在AngularJS项目中访问.net Web.Config - Accessing .net Web.Config in AngularJS project AngularJS读取web.config值 - AngularJS Read web.config value 有没有办法在 Angular 中动态选择 web.config? - Is there a way to dynamically choose web.config in Angular? AngularJS和web.config重定向冲突 - AngularJS and web.config Redirect conflicts 遵循AngularJS / MySQL / PHP教程。 无法将给定的.HTACCESS文件转换为等效的web.config - Following an AngularJS/MySQL/PHP tutorial. Having trouble converting the given .HTACCESS file to an equivalent web.config 如何在MVC控制器方法中读取web.config设置并在angularjs视图,控制器和服务中访问它们? - How to read web.config settings in MVC controller method and access them in angularjs view, controller and service? Web API项目的web.config中的额外键 - Extra keys in web.config of web api project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM