Nodejs / Net-SNMP:错误向客户端发送snmp-walk响应

问题描述 投票:0回答:1

我想通过snmp创建监控服务器的项目。我使用net-snmp组件和express.i有错误“无法在发送到客户端后设置标头”当我想发送响应snmp walk to client。

我写了代码。

var snmp = require('net-snmp');
var express = require('express');
app.on('/api/snmp/', (req, res) => {
  const session = new snmp.createSession(req.query.IP, req.query.Community);
  session.walk(
    '1.3.6.1',
    20,
    function(varbinds) {
     let msg = [];
      for (var i = 0; i < varbinds.length; i++) {
        if (snmp.isVarbindError(varbinds[i])) {
          console.log('error1');
        } else {
          console.log('send');
          msg.push({ message: varbinds[i].oid + '|' + varbinds[i].value });
        }
      }
      console.log(res.getHeaders());
      // res.setHeader('Content-Type', 'application/json');
      // res.send(msg);
      res.end(msg);
      // resolve(resultStr);
    },
    function(error) {
      console.log('error2');
      console.log(error.toString()); // show error in console: Cannot set headers after they are sent to the client
    }
  );
});
node.js net-snmp
1个回答
0
投票

当我将“msg”变量的类型转换为有效的字符串时,我可以找到解决方案。但我不知道为什么这有问题的对象类型。

© www.soinside.com 2019 - 2024. All rights reserved.