【问题标题】:iisnode and cors on azure websitesazure 网站上的 iisnode 和 cors
【发布时间】:2015-09-23 01:37:36
【问题描述】:

我有一个 nodejs/expressjs Web API 项目设置,当我使用 curl 对其进行测试时,它运行良好。

这是本地调用的输出:

dc-designer-client git:(master) curl -H "Origin: http://example.com" -H "Access-Control-Request-Method: GET" -H "Access-Control-Request-Headers: X-Requested-With" -X OPTIONS --verbose http://0.0.0.0:3000/api/grid
* Hostname was NOT found in DNS cache
*   Trying 0.0.0.0...
* Connected to 0.0.0.0 (127.0.0.1) port 3000 (#0)
> OPTIONS /api/grid HTTP/1.1
> User-Agent: curl/7.37.1
> Host: 0.0.0.0:3000
> Accept: */*
> Origin: http://example.com
> Access-Control-Request-Method: GET
> Access-Control-Request-Headers: X-Requested-With
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
< Access-Control-Allow-Headers: Content-Type, Authorization, Content-Length, X-Requested-With, *
< Content-Type: text/plain; charset=utf-8
< Content-Length: 2
< ETag: W/"2-4KoCHiHd29bYzs7HHpz1ZA"
< Date: Mon, 06 Jul 2015 01:24:01 GMT
< Connection: keep-alive
<
* Connection #0 to host 0.0.0.0 left intact

但是,当我部署到 azure 并尝试执行相同的 curl 语句时,它给了我以下信息:

dc-designer-server git:(master) curl -H "Origin: http://example.com" -H "Access-Control-Request-Method: GET" -H "Access-Control-Request-Headers: X-Requested-With" -X OPTIONS --verbose http://dc-server.azurewebsites.net/api/grid
* Hostname was NOT found in DNS cache
*   Trying 23.96.124.25...
* Connected to dc-server.azurewebsites.net (127.0.0.1) port 80 (#0)
> OPTIONS /api/grid HTTP/1.1
> User-Agent: curl/7.37.1
> Host: dc-server.azurewebsites.net
> Accept: */*
> Origin: http://example.com
> Access-Control-Request-Method: GET
> Access-Control-Request-Headers: X-Requested-With
>
* Empty reply from server
* Connection #0 to host dc-server.azurewebsites.net left intact
curl: (52) Empty reply from server

我确信这与 iisnode 和 iis on azure 有关。我不知道的是如何配置 Azure 网站以允许 OPTIONS 预检检查通过 nodejs。

我的 server.js 代码正在使用 cors npm 包。当我配置 nodejs 和 express 时,此代码在本地工作,但 Azure 上的 IIS 似乎阻塞或导致我的代码出现问题。

是否有人使用 iisnode 配置 Azure 网站以允许 CORS 调用?

【问题讨论】:

标签: node.js azure express cors iisnode


【解决方案1】:

有一个示例here 同时设置了前端和后端站点,并使用 CORS 进行设置。具体来说,this file 有逻辑。

话虽如此,这是一个 .NET 示例。所以说明它一般可以在 Azure Web Apps 上运行,但我不确定是否有与 IISNode 相关的特殊注意事项。

【讨论】:

  • 谢谢大卫,你有没有对 IISNode 和 CORS 做同样事情的例子?我正在使用 CORS npm 包,我知道它可以在 Azure 之外运行。我真的认为 IIS 正在阻止或不向 NodeJS 发送 CORS 请求。对于这个特定场景的任何帮助将不胜感激。
【解决方案2】:

我在 Azure 网站上使用 nodejs。我已经安装了 cors 包并启用了来自所有域的请求。 Azure 仍在阻止 CORS 请求。

我更改了 web.config(每个站点都有一个,如果一个不是部署的一部分,则创建一个)。根据这个 stackoverflow 帖子:HTTP OPTIONS request on Azure Websites fails due to CORS

下面是我完整的 web.config - 有针对我的网站的设置,因此无法复制和粘贴。

<?xml version="1.0" encoding="utf-8"?>
<!--
     This configuration file is required if iisnode is used to run node processes behind
     IIS or IIS Express.  For more information, visit:

     https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->

<configuration>
  <system.webServer>
    <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
    <webSocket enabled="false" />
    <handlers>
      <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
      <add name="iisnode" path="transpiled/www.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="^transpiled/www.js\/debug[\/]?" />
        </rule>

        <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}"/>
        </rule>

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

    <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <!-- Make sure error responses are left untouched -->
    <httpErrors existingResponse="PassThrough" />

    <!--
      You can control how Node is hosted within IIS using the following options:
        * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
        * node_env: will be propagated to node as NODE_ENV environment variable
        * debuggingEnabled - controls whether the built-in debugger is enabled

      See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
    -->
    <!--<iisnode watchedFiles="web.config;*.js"/>-->

    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,OPTIONS" />
            <add name="Access-Control-Allow-Headers" value="Origin, X-Olaround-Debug-Mode, Authorization, Accept" />
            <add name="Access-Control-Expose-Headers" value="X-Olaround-Debug-Mode, X-Olaround-Request-Start-Timestamp, X-Olaround-Request-End-Timestamp, X-Olaround-Request-Time, X-Olaround-Request-Method, X-Olaround-Request-Result, X-Olaround-Request-Endpoint" />
        </customHeaders>
    </httpProtocol>

  </system.webServer>
</configuration>

复制此部分并将其插入到 关闭 system.webServer 标记的正上方。

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,OPTIONS" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Olaround-Debug-Mode, Authorization, Accept" />
        <add name="Access-Control-Expose-Headers" value="X-Olaround-Debug-Mode, X-Olaround-Request-Start-Timestamp, X-Olaround-Request-End-Timestamp, X-Olaround-Request-Time, X-Olaround-Request-Method, X-Olaround-Request-Result, X-Olaround-Request-Endpoint" />
    </customHeaders>
</httpProtocol>

要获取 web.config,您必须先部署并通过 FTP 进入实例。您可以通过单击站点仪表板中的“下载发布配置文件”链接来执行此操作。

打开文件,是xml。

<?xml version="1.0"?>
<publishData>
    <publishProfile profileName="secret - Web Deploy" publishMethod="MSDeploy" publishUrl="secret.scm.azurewebsites.net:443" msdeploySite="secret" userName="$secret" userPWD="v0ifRuLpeXf42kurCFHqXSA5uQnAdmx2c7lCHrrQPiyB6TxlXoG0dfJGFndH" destinationAppUrl="http://secret.azurewebsites.net" SQLServerDBConnectionString="" mySQLDBConnectionString="" hostingProviderForumLink="" controlPanelLink="" webSystem="WebSites">
    <databases/>
</publishProfile>
<publishProfile profileName="secret - FTP" publishMethod="FTP" publishUrl="ftp://secret.ftp.azurewebsites.windows.net/site/wwwroot" ftpPassiveMode="True" userName="secret\$secret" userPWD="secret2c7lCHrrQPiyB6TxlXosecret" destinationAppUrl="http://secret.azurewebsites.net" SQLServerDBConnectionString="" mySQLDBConnectionString="" hostingProviderForumLink="" controlPanelLink="" webSystem="WebSites">
<databases/>
</publishProfile>
</publishData>

在 FTP 的 publishProfile 元素中,您可以找到 url、用户名和密码。使用完整的值,否则将不起作用。

获得 web.config 后,进行更改并将其上传回网站。看看这是否有效。它对我有用。

为了让它继续工作,您需要将 web.config 添加到项目(或站点)的根目录。否则,Azure 将在每次部署时将其淘汰。

【讨论】:

  • 嗨 Chuck,我不熟悉 X-Olaround-* 标头。另外,我是否需要在管理控制台上执行任何操作以获取 web.config 更改?最后,我没有使用自定义的“转译”文件夹,这是必要的还是子文件夹?
  • 不,会自动检测到 web.config 更改。 transpile 文件夹特定于我的应用程序。这就是为什么我建议您从您的 Azure 网站实例登录下载 web.config。在我的应用程序中,transpile/www.js 是我的启动 javascript 文件。您可以尝试将其更改为指向您的启动脚本。
  • 我已经尝试了 customheaders 部分并验证了 web.config 是否已根据我的更改进行了更新。它仍然不起作用?当我使用 OPTIONS 标头 curl 时,IIS 立即响应以下响应:
  • “服务器回复空”
  • Chuck,您使用的是什么类型的服务器?我正在使用 Basic 1: Small 实例。因为它是一个共享环境,这可能是问题吗?
【解决方案3】:

事实证明,您无需修改​​ Azure IISNode 的默认 web.config 即可处理 CORS 请求。

在我的 server.js NodeJS 代码中,我使用标准的 CORS npm 包。为了让我的 CORS 请求能够跨浏览器工作,我必须进行更改的是将请求的协议从“HTTP”更改为“HTTPS”。

进行此更改后,我可以使用任何浏览器以及使用 curl 测试我的 WebAPI。

【讨论】:

  • 不要把这个信息像答案一样你可以添加这个信息编辑你的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多