node.js文件怎么接收前端页面传过来的值,和前端怎么给node.js传值,目前我可以做到从node.js取到值,但是不能由前端传值给后端
这是前端的请求
axios({
url: "http://localhost:3000/api/table",
method: "GET"
}).then((ok) => {
console.log(ok.data.data.list)
for (let i = 0; i < ok.data.data.list.length; i++) {
if(ok.data.data.list[i][0]!=0){
this.DeviceAlarmData.push({
clock: "Core",
severity: ok.data.data.list[i][1],
r_clock: "System Error",
r_eventid: this.SnmpTime(ok.data.data.list[i][2]),
IPname: ok.data.data.list[i][4]
})
}
}
console.log(this.DeviceAlarmData)
}).catch((err) => {
console.log(err)
})
这是node.js的代码
app.get('/api/table', (req, res) => {
console.log(req)
let oid = "1.3.6.1.4.1.52126.1.1.2.1";
let session = snmp.createSession("192.168.3.155", "txwrite");
let maxRepetitions = 20;
session.table(oid, maxRepetitions, function(error, table) {
if (error) {
console.error(error.toString());
} else {
// This code is purely used to print rows out in index order,
// ifIndex's are integers so we'll sort them numerically using
// the sortInt() function above
var indexes = [];
for (index in table)
indexes.push(parseInt(index));
indexes.sort(sortInt);
// Use the sorted indexes we've calculated to walk through each
// row in order
let arr = []
for (var i = 0; i < indexes.length; i++) {
// Like indexes we sort by column, so use the same trick here,
// some rows may not have the same columns as other rows, so
// we calculate this per row
var columns = [];
for (column in table[indexes[i]])
columns.push(parseInt(column));
columns.sort(sortInt);
arr.push([])
for (var j = 0; j < columns.length; j++) {
arr[i].push("" + table[indexes[i]][columns[j]])
}
// Print index, then each column indented under the index
console.log("table " + table)
}
res.json({
code: 200,
message: '成功',
data: {
list: arr
}
});
}
session.close();
});
})