如何在Azure应用服务上调试Node.js应用-获取http状态码500

时间:2019-06-29 21:41:24

标签: node.js windows azure azure-web-sites

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

  

“(节点:25936)[DEP0005] DeprecationWarning:由于安全性和可用性问题,不建议使用Buffer()。请改用Buffer.alloc(),Buffer.allocUnsafe()或Buffer.from()方法。 “

配置:

{
  "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"
  }
}

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="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');
});

2 个答案:

答案 0 :(得分:1)

您可以启用node.js application level log来查看错误消息。

您在此处粘贴的日志仅是警告,不是错误。你可以找到  代码中新的Buffer()方法,并用新方法替换它们,以免发生这种情况。

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

以下是关于guidedeploying nodejs app to azure,供您参考。

答案 1 :(得分:0)

正如Caiyi Ju所说,您需要侦听process.env.PORT变量指定的端口。这是Azure为您的应用程序提供端口号的一种方式。