我在硬件上与控制台连接 但它与命令一起工作:
我创建了omron.sh,因为我需要在nodeJS
中运行它#!/bin/sh
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/splincode/Develop/omron/c++_modules/libomron/omron-build/lib/
~/Develop/omron/usr/local/bin/omron_790IT_test
但我需要它在NodeJS中工作并写入流stdout,但是,不工作
server.js:
let http = require('http'),
fs = require('fs'),
url = require('url'),
path = require('path');
let server = new http.Server(); // EventEmitter
server.listen(8000, '127.0.0.1');
server.on("connection", () => console.log("connection"));
server.on('request', (request, response) => {
console.log('.', request.url)
let userUrl = decodeURIComponent(url.parse(request.url).pathname);
let loadFile = false;
switch(userUrl) {
case '/':
userUrl += 'index.html';
loadFile = true;
break;
case '/omron':
console.log('omron')
var util=require('util')
var exec=require('child_process').spawn('sh', ['omron.sh']);
// not work
exec.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
response.end(`${data}`);
});
break;
default:
loadFile = true;
break;
};
if (userUrl === '/') userUrl += 'index.html';
if (loadFile) {
let pathname = path.normalize(path.join(__dirname, userUrl));
fs.readFile(pathname, (err, content) => {
let mime = require('mime').lookup(pathname);
response.setHeader('Content-Type', `${mime}; charset=utf-8`);
response.end(content);
});
}
});
但是,不行,我需要给shell脚本一个响应输出流,但是怎么做?
答案 0 :(得分:1)
节点-CMD
Simple commandline/terminal interface to allow you to run cli or
bash style commands as if you were in the terminal.
异步运行命令,如果需要,可以将输出作为字符串。
所以做这样的事情来解决你的问题
var cmd=require('node-cmd');
cmd.get(
'pwd',
function(data){
console.log('the current working dir is : ',data)
}
);
cmd.run('whoami');