【问题标题】:Node.js on Azure Websites 404 ErrorAzure 网站上的 Node.js 404 错误
【发布时间】:2023-04-11 01:01:02
【问题描述】:

我在 Azure 网站上运行 Node.js 0.10.15 和 Express 应用程序,当有人输入无效 URL 时发送 404 时出现问题。当我尝试访问无效的 URL 时,我收到了一般的 The page cannot be displayed because an internal server error has occurred. 错误。

这是我的app.js文件末尾的代码,用于发送404页面:

app.get('/*', function(req, res) { res.status(404).sendfile('public/404.html'); });

奇怪的是,当我在本地运行我的服务器时,Express 似乎正确地发送了 404 以及 HTML 文件,但是当它在 Azure 上运行时,它阻塞并且没有给我一个有用的错误。日志显示请求按预期工作:

[Thu, 24 Oct 2013 01:10:39 GMT] "GET /asdfsadf HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"

我怀疑问题出在我的web.config 文件中:

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <system.webServer>
      <httpErrors existingResponse="PassThrough" />
      <staticContent>
         <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
         <mimeMap fileExtension=".woff" mimeType="application/x-woff" />
         <mimeMap fileExtension=".ttf" mimeType="application/x-woff" />
      </staticContent>
      <handlers>
        <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
      </handlers>
      <rewrite>
        <rules>
            <rule name="DynamicContent">
                 <match url="/*" />
                 <action type="Rewrite" url="app.js"/>
            </rule>
       </rules>
      </rewrite>
    </system.webServer>
  </configuration>

我还使用iisnode.yml 文件打开了日志记录:

loggingEnabled: true
devErrorsEnabled: true

另外,您可以在 GitHub 上查看我的开发工作分支

【问题讨论】:

    标签: node.js azure express azure-web-app-service


    【解决方案1】:

    Windows Azure Websites is overriding my 404 and 500 error pages in my node.js apphttps://github.com/tjanczuk/iisnode/issues/295

    两者都为我工作。我的 express 应用程序没有任何 Web.config,这就是我使用的:

    <configuration>
        <system.webServer>
          <handlers>
            <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
          </handlers>
          <rewrite>
               <rules>
                    <rule name="DynamicContent">
                         <conditions>
                              <add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="True"/>
                         </conditions>
                         <action type="Rewrite" url="server.js"/>
                    </rule>
               </rules>
          </rewrite>
          <httpErrors existingResponse="PassThrough"/>
        </system.webServer>
    </configuration>
    

    【讨论】:

      【解决方案2】:

      当我遇到这个对我有帮助的问题时

      <staticContent>
          <remove fileExtension=".svg" />
          <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
      
          <remove fileExtension=".woff" />
          <mimeMap fileExtension=".woff" mimeType="application/application/font-woff" />
      
          <remove fileExtension=".ttf" />
          <mimeMap fileExtension=".ttf" mimeType="application/x-font-truetype" />
      
          <remove fileExtension=".otf" />
          <mimeMap fileExtension=".otf" mimeType="application/x-font-opentype" />
      </staticContent>
      

      因此,在声明 mimeMap 之前,必须确保默认情况下未注册 mimeType,这就是您可以删除 mimeType 然后再注册它的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-02
        • 1970-01-01
        • 1970-01-01
        • 2017-03-05
        • 1970-01-01
        • 1970-01-01
        • 2013-06-14
        相关资源
        最近更新 更多