Nodejs /Net-SNMP:向客户端发送 snmp-walk 响应时出错
Nodejs /Net-SNMP :Error send snmp-walk response to client
我想通过snmp创建监控服务器的项目。我使用 net-snmp 组件并且 express.i 有错误 "Cannot set headers after they are sent to the client" 当我想向客户端发送响应 snmp walk 时。
我写代码
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
}
);
});
当我将 "msg" 变量的类型转换为可用的字符串时,我可以找到解决方案。
但我不知道为什么这有问题对象类型。
我想通过snmp创建监控服务器的项目。我使用 net-snmp 组件并且 express.i 有错误 "Cannot set headers after they are sent to the client" 当我想向客户端发送响应 snmp walk 时。
我写代码
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
}
);
});
当我将 "msg" 变量的类型转换为可用的字符串时,我可以找到解决方案。 但我不知道为什么这有问题对象类型。