var ls= require('child_process').spawn('ping', ['www.baidu.com']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
17 回复
E:\Workspace\TZ\LTE-Test\client>npm install iconv
npm http GET http://registry.npmjs.vitecho.com/iconv
npm http 304 http://registry.npmjs.vitecho.com/iconv
> [email protected] install E:\Workspace\TZ\LTE-Test\client\node_modules\iconv
> node-gyp rebuild
E:\Workspace\TZ\LTE-Test\client\node_modules\iconv>node "C:\Program Files\nodejs
\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js"
rebuild
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.Cpp.InvalidPlatform
.Targets(23,7): error MSB8007: The Platform for project 'iconv.vcxproj' is inva
lid. Platform='x64'. You may be seeing this message because you are trying to
build a project without a solution file, and have specified a non-default Platf
orm that doesn't exist for this project. [E:\Workspace\TZ\LTE-Test\client\node_
modules\iconv\build\iconv.vcxproj]
gyp ERR! build error
gyp ERR! stack Error: `C:\windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
` failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\
npm\node_modules\node-gyp\lib\build.js:215:23)
gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:91:17)
gyp ERR! stack at Process._handle.onexit (child_process.js:674:10)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modu
les\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd E:\Workspace\TZ\LTE-Test\client\node_modules\iconv
gyp ERR! node -v v0.8.7
gyp ERR! node-gyp -v v0.6.5
gyp ERR! not ok
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! `cmd "/c" "node-gyp rebuild"` failed with 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is most likely a problem with the iconv package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get their info via:
npm ERR! npm owner ls iconv
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "iconv"
npm ERR! cwd E:\Workspace\TZ\LTE-Test\client
npm ERR! node -v v0.8.7
npm ERR! npm -v 1.1.49
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! E:\Workspace\TZ\LTE-Test\client\npm-debug.log
npm ERR! not ok code 0
PS: 如何判断终端的编码呢? 譬如这段程序可能需要在linux和win下都运行, 如何兼容?
最终的实现:
"use strict";
var spawn = require('child_process').spawn;
var iconv = require('iconv-lite');
var BufferHelper = require('bufferhelper');
exports.ping = function(address, count, callback){
var ping = spawn('ping', [address,'-n',count||5]);
var bufferHelper = new BufferHelper();
ping.stdout.on('data', function(chunk){
bufferHelper.concat(chunk);
});
ping.stdout.on('end',function(){
var str = iconv.decode(bufferHelper.toBuffer(),'gbk');
callback(null,str);
});
}
不知道怎么获取终端的编码。貌似Linux一般都是UTF-8的,只有Windows才是ANSI编码,而且若终端输出的是英文字符,用哪个编码方式都是可以正常显示的。 如果是这样的话,简单地判断一下操作系统类型吧
@leizongmin 也只能不严谨了. 本想判断操作系统类型, 终端编码, 和终端的语言的. 那「终端的语言」这个怎么判断呢? 或者能否指定终端的lang? 中文的输出和英文的输出,字符串分析是不一样的。 (win肯定是中文的了,linux就怕有些是e文的),暂时想到的办法是正则式判断是否存在中文。