path os child_process 모듈


path - 파일 및 폴더 경로 핸들링

path 모듈은 파일 및 디렉토리 경로의 작업을 위한 유틸리티를 제공합니다. 파일의 경로를 읽어올 경우 운영체제 별로 경로의 정보가 다릅니다.

Windows 운영체제와 POSIX 타입의 운영체제들은 서로 파일의 경로를 표현하는 방법이 다르기에 path 모듈을 이용해 운영체제가 다르더라도 올바른 경로 정보를 관리할 수 있습니다.

const path = require("path")


/* file의 확장자를 리턴 */
path.extname(file)
path.extname('index.html');  // '.html'


/* 파일 패스에서 디렉터리 이름을 반환 */
path.dirname()


/* 파일의 확장자를 제외한 이름을 반환 */
path.basename()


/* 여러개의 경로명들을 하나의 파일 패스로 반환 */
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
//-> '/foo/bar/baz/asdf'

path.resolve('/foo/bar', './baz');
//-> '/foo/bar/baz'

path.resolve('/foo/bar', '/tmp/file/');
//-> '/tmp/file'


/* 패스 세그먼트 분리자 - win: \,  posix: / */
path.sep
'foo/bar/baz'.split(path.sep)    //-> ['foo', 'bar', 'baz']

os - 시스템 정보 제공

os 모듈은 노드 애플리케이션을 실행하는 컴퓨터의 운영체제에 관한 정보를 제공합니다.

const os = require('os');

os.hostname()             // 운영체제 호스트 이름 DESKTOP-MH
os.type()                 // 운영체제의 종류 Linux
os.platform()             // 플랫폼 종류 linux
os.release()              // 운영체제 버전 3.13.-052-gen
os.arch()                 // 운영체제 아키텍처 x64
os.cpus().length          // 코어 갯수 1
os.networkInterfaces()    // 네트워크 인터페이스 정보를 담은 배열
(os.uptime()/60/60/24).toFixed(1)  // 운영체제 실행시간 80.3 (days)
(os.totalmem()/1e6).toFixed(1)     // 전체 메모리 용량 1042.2 (MB)
(os.freemem()/1e6).toFixed(1)      // 가용 메모리 용량 124.2 (MB)

child_process - 자식 프로세스

child_process 모듈은 노드 앱에서 다른 프로그램을 실행할 때 사용합니다.

exec - shell 명령을 호출합니다. 명령줄에서 실행할 수 있는 것은 무엇이든 실행할 수 있습니다.

// ls 명령 사용하기
const exec = require('child_process').exec;

exec('ls', function(err, stdout, stderr) {
  if(err) return console.error('Error: "ls" Cannot Excuting');
  stdout = stdout.toString();
  console.log(stdout);
  
  stderr = stderr.toString();
  if(stderr !== '') {
    console.error('Error: ' + stderr);
  }
})

execFile - shell을 통하지 않고 실행 파일을 직접 실행합니다.

fork - 다른 노드 스크립트를 실행할 때 사용합닏. 각각의 프로세스 간 커뮤니케이션도 가능합니다.

SHARE