javascript - TypeError:将循环结构转换为 JSON 以获取 shell 脚本结果
问题描述
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const shell = require('shelljs');
var sys = require('sys')
var exec = require('child_process').exec;
app.get('/api/v1/script', (request, response) =>{
//console.log("HERE")
var extra = "172.30.82.147 0000023FC160 qam"
const ls = exec('source /root/chat-box/ip.sh ' + extra,
(error, stdout, stderr) => {
console.log(`${stdout}`);
console.log(`${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
console.log("$IP")
const messages = process.env
response.json({ ls });
});
这段代码应该给我一个 IP,但我从中得到的只是
TypeError:在 ServerResponse.json (/root/chat-box/node_modules /express/lib/response.js:260:14) at app.get (/root/chat-box/server.js:31:12) at Layer.handle [as handle_request] (/root/chat-box/node_modules /express/lib/router/layer.js:95:5) 在下一个 (/root/chat-box/node_modules/express/lib/router/route.js:137:13) 在 Route.dispatch (/root/chat -box/node_modules/express/lib/router/route.js:112:3) 在 Layer.handle [as handle_request] (/root/chat-box/node_modules/express/lib/router/layer.js:95:5 ) 在 /root/chat-box/node_modules/express/lib/router/index.js:281:22 在 Function.process_params (/root/chat-box/node_modules/express/lib/router/index.js:335: 12)
但我也无法打印任何东西来看看出了什么问题
解决方案
推荐阅读
- arangodb - ArangoDB 分片集群性能问题
- c# - 尝试连接到服务时出现 TLS 错误
- r - 映射数据框列表
- firebase - 无法通过 API 检索 Google Analytics for Firebase
- r - R图泊松分布和负二项式
- javascript - 谷歌地图:删除标记集群
- typescript - 用于 Vue 3 插件的增强类型
- sql - 返回与另一个表中的所有行至少有一个引用的所有行
- apache-kafka - 如何在我的 docker-compose 文件中为 db2 数据库连接配置带有“securityMechanism=9,encryptionAlgorithm=2”的 kafka-connect?
- ios - iOS 模拟器颜色出错