Node.js
[Node.js] 내장모듈 사용하기(시스템 정보, 파일Path 알려주는 os모듈)
Sorrynthx
2019. 8. 5. 21:46
서버 기능을 만들다 보면 시스템의 CPU나 메모리, 디스크 용량을 확인할 수 있는 os 모듈
메소드 | 설명 |
hostname() | 운영체제의 호스트 이름을 알려줌 |
totalmem() | 시스템 전체의 메모리 용량 알려줌 |
freemem() | 시스템에서 사용 가능한 메모리 알려줌 |
cpus() | CPU 정보를 알려줌 |
networkInterfaces() | 네트워크 인터페이스 정보를 담은 배열 객체 반환 |
내장모듈을 사용하기 위해 require() 함수를 호출하여 os 내장모듈 불러온다.
var os = require('os');
console.log('시스템의 hostname : %s', os.hostname());
console.log('시스템의 메모리 : %d / %d', os.freemem(), os.totalmem());
console.log('시스템의 CPU정보 : \n');
console.dir(os.cpus());
console.log('시스템의 네트워크 인터페이스 정보\n');
console.dir(os.networkInterfaces());
결과
파일 패스를 다루는 Path 모듈
메소드 | 설명 |
join() |
여러 개의 이름들을 모두 합쳐 하나의 파일 패스로 만들어줌 파일 패스를 완성할 때 구분자 등을 알어서 조정함
|
dirname() | 파일 패스에서 디텍터리 이름을 반환함 |
basename() | 파일 패스에서 파일의 확장자를 제외한 이름을 반환함 |
extname() | 파일 패스에서 파일의 확장자를 반환함 |
var path = require('path');
//디렉터리 이름 합치기
var directories = ['users','mike','docs'];
var docsDirectory = directories.join(path.sep); // '\'기준으로 join 시킴
console.log('문서 디텍터리 : %s', docsDirectory);
//디렉터리 이름과 파일 이름 합치기
var curPath = path.join('/Users/sorryn', 'notepad.exe');
console.log('파일 패스 : %s', curPath);
//패스에서 디텍터리, 파일, 이름, 확장자 구별하기
var filename = 'C\\Users\\sorryn\\notepad.exe';
var dirname = path.dirname(filename);
var basename = path.basename(filename);
var extname = path.extname(filename);
console.log('디텍터리 : %s, 파일 이름 : %s, 확장자 : %s', dirname, basename, extname);
결과