如何在azure应用程序服务上调试nodejs应用程序 - 获取http状态代码500

2024-03-18

我是nodejs的新手。我有一个基本的nodejs应用程序,可以在带有nodejs v10.15.0和npm v6.9.0的Windows PC上正常运行,但是当我将其部署到节点版本10.15.2的Windows平台上的Azure应用服务时,我得到500内部服务器错误。 我尝试使用 web.config 中的 iisnodeloggingEnabled 标志进行调试,我可以看到正在记录以下消息。我不确定是警告还是错误导致了 500。

“(节点:25936)[DEP0005] DeprecationWarning:由于安全和可用性问题,Buffer() 已被弃用。请改用 Buffer.alloc()、Buffer.allocUnsafe() 或 Buffer.from() 方法。”

Config :

{
  "name": "redis_test_app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" 
             && exit 1",
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "applicationinsights": "^1.4.0",
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "livereload": "^0.7.0",
    "redis": "^2.8.0"
  }
}

网络配置

<?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="index.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="^index.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="index.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 flushResponse="true" loggingEnabled="true"  logDirectory="iisnode" watchedFiles="web.config;*.js"/>
  </system.webServer>
</configuration>

app.js

const appInsights = require("applicationinsights");
appInsights.setup("")
.setAutoDependencyCorrelation(true)
    .setAutoCollectRequests(true)
    .setAutoCollectPerformance(true)
    .setAutoCollectExceptions(true)
    .setAutoCollectDependencies(true)
    .setAutoCollectConsole(true)
    .setUseDiskRetryCaching(true)
    .start();

var redis = require('redis');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var client = redis.createClient(xxxx, 'gfkdhggk865487766jggdfgdf', {
    auth_pass: 'passcode',
    tls: { servername: 'xxxxxxxx' }
  });
var config = require('./server.config');
var port = config.port;

// allow cross origin 
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/', require('./src/routes')());

app.listen(port, function () {
    console.log(`Maintenance server listening on port ${port}!`)
});

// check if the client is connected to redis
client.on('connect', function () {
    console.log('Connected to Redis');
});

您可以启用node.js应用程序级别日志 https://blogs.msdn.microsoft.com/azureossds/2018/08/03/debugging-node-js-apps-on-azure-app-services/查看错误消息。

您粘贴到此处的日志只是警告,而不是错误。你可以找到 在代码中添加新的 Buffer() 方法并将其替换为新方法以避免这种情况。

确保将项目部署到 Azure 时已更改侦听端口。你应该使用app.listen(process.env.PORT);在你的 app.js 文件中。

这里有一个guide https://learn.microsoft.com/en-us/azure/app-service/app-service-web-get-started-nodejs#deploy-zip-file关于将nodejs应用程序部署到azure https://learn.microsoft.com/en-us/azure/app-service/app-service-web-get-started-nodejs#deploy-zip-file供你参考。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在azure应用程序服务上调试nodejs应用程序 - 获取http状态代码500 的相关文章

随机推荐