调试 Node.js 代理和对其进行问题排查

您正在查看 Apigee Edge 文档。
前往 Apigee X 文档
信息

在将任何 Node.js 代码部署到 Edge 之前,最好先确保这些代码能够正常运行。本主题讨论了在部署包含 Node.js 应用的代理后,如何对其进行调试和问题排查。

增加连接池大小以提高性能

如果您发现性能缓慢,可以通过使用 maxSockets 增加默认的 Node.js 连接池大小,或者通过完全停用套接字池来解决此问题。如需了解详情和查看示例代码,请参阅 Apigee 社区中的 Node.js 性能提示:套接字池

查看 Node.js 日志

如需查看有关 Node.js 应用的日志信息,请执行以下操作:

  1. 在主 API 代理页面中,点击您要查看的代理。
  2. 在所选代理的摘要页面中,点击工具栏右侧的 Node.js 日志

在“日志”页面中,您可以选择要查看的日志的时间范围,如下所示。日志会记录 HTTP 方法调用、调用的成功或失败、console.log 消息等。在搜索字段中输入搜索字符串,以显示包含该字符串的所有日志条目。

使用轨迹工具

跟踪记录工具可用于常规代理调试。如需详细了解如何使用 Trace 工具,请参阅使用 Trace 工具

打印控制台输出

您可以在 Node.js 代码中嵌入 console.log 语句,并在跟踪工具中查看输出。例如,以下语句会输出变量 config.user 的值:

console.log('Logging in as %s', config.username);

如需在 Trace 工具中查看 console.log 消息的输出,请在 Trace 工具中调用您的 API,然后点击从所有事务输出 以打开输出面板:

假设您执行了以下代码...

var http = require('http');

console.log('node.js application starting...');

var svr = http.createServer(function(req, resp) {
    resp.end('Hello, World!');
});

svr.listen(9000, function() {
    console.log('Node HTTP server is listening');
});

... 控制台语句会显示在面板中:

配置 Nginx 目标超时设置

如果您使用 Nginx 作为代理并看到“Bad Gateway”错误,请尝试增加 Nginx 代理超时配置,如下所述:

例如:

proxy_connect_timeout       60;
proxy_read_timeout          120;

服务器响应所需的时间可决定理想的超时配置。例如,45-60 秒的读取超时时间可能非常适合提供合理的缓冲。

配置 Apigee 目标超时设置

您还可以在 TargetEndpoint 的 HttpTargetConnection 元素中配置 Apigee 默认超时值。默认值为:

connect.timeout.millis - 60 seconds
io.timeout.millis - 120 seconds
<HTTPTargetConnection>
    <Properties>
        <Property name="connect.timeout.millis">5000</Property>
        <Property name="io.timeout.millis">5000</Property>
    </Properties>
    <URL>http://www.google.com</URL>
</HTTPTargetConnection>

另请参阅端点属性参考文档

了解详情

如需详细了解调试,请参阅:

后续步骤

如需了解 Apigee Edge 中的 Node.js 模块支持,包括有关 HTTP/HTTPS、TLS 支持和其他高级主题的信息,请参阅了解 Edge 对 Node.js 模块的支持