exec(‘dir’,{ encoding: ‘utf8’, timeout: 100000, maxBuffer: 200*1024, killSignal: ‘SIGTERM’, cwd: null, env: null }, function(error,stdout,stderr){ response.writeHead(200, {‘Content-Type’: ‘text/plain;charset:utf-8’}); console.log(stdout); response.write(stdout); response.end(); });
输出的内容是乱码如何解决?
确保js文件是utf-8
var exec = require(‘child_process’).exec; var iconv = require(‘iconv-lite’);
exec(‘dir’,{ encoding: ‘binary’, timeout: 100000, maxBuffer: 200*1024, killSignal: ‘SIGTERM’, cwd: null, env: null }, function(error,stdout,stderr){ var str = iconv.decode(stdout, ‘GBK’); console.log(str); });
中文乱码问题也可以参考这里 http://www.9958.pw/post/nodejs_gbk
response.writeHead(200, {‘Content-Type’: ‘text/plain;charset=utf-8’});
把:改成=