본문 바로가기

Back-end & Server/Node.js

[Node.js] Node.js의 핵심 기능

728x90
반응형

Buffer

 

Buffer | Node.js v20.5.0 Documentation

 

nodejs.org

Buffer는 Node.js에서 바이너리 데이터를 다루는 데 사용되는 객체이다.

Node.js는 JavaScript가 문자열을 기본으로 다루기 때문에 바이너리 데이터를 다루는 데에는 적합하지 않다.

이때 Buffer를 사용하여 고정 길이의 바이트 배열로 바이너리 데이터를 다룰 수 있다.

 

예시

// Buffer 생성
const buffer = Buffer.from('Hello, World!', 'utf-8');

// Buffer의 데이터 출력
console.log(buffer.toString('utf-8')); // 출력: Hello, World!

 

Events

 

Events | Node.js v20.5.0 Documentation

Events# Source Code: lib/events.js Much of the Node.js core API is built around an idiomatic asynchronous event-driven architecture in which certain kinds of objects (called "emitters") emit named events that cause Function objects ("listeners") to be call

nodejs.org

Events는 Node.js에서 이벤트 기반 프로그래밍을 구현하는 데 사용되는 모듈이다.

이벤트를 발생시키고, 리스너를 등록하여 해당 이벤트가 발생할 때 콜백 함수를 실행할 수 있다.

이를 통해 비동기적인 이벤트 처리를 할 수 있다.

 

예시

const EventEmitter = require('events');

// 이벤트 객체 생성
const eventEmitter = new EventEmitter();

// 이벤트 리스너 등록
eventEmitter.on('greet', () => {
  console.log('Hello, there!');
});

// 이벤트 발생
eventEmitter.emit('greet'); // 출력: Hello, there!

 

 

Stream

 

Stream | Node.js v20.5.0 Documentation

 

nodejs.org

Stream은 Node.js에서 데이터 처리를 위해 사용되는 기능으로, 대용량 데이터를 작은 덩어리로 나눠서 처리하는 방식이다.

파일 읽기/쓰기, 네트워크 통신 등에서 많이 활용된다.

 

예시

const fs = require('fs');

// Readable Stream으로 파일 읽기
const readStream = fs.createReadStream('input.txt', 'utf-8');

// Writable Stream으로 파일 쓰기
const writeStream = fs.createWriteStream('output.txt');

// 데이터를 읽어서 쓰기 스트림으로 전달
readStream.pipe(writeStream);

 

 

Process

 

Process | Node.js v20.5.0 Documentation

Process# Source Code: lib/process.js The process object provides information about, and control over, the current Node.js process. import process from 'node:process';const process = require('node:process');copy Process events# The process object is an inst

nodejs.org

Process는 Node.js에서 현재 실행 중인 프로세스 정보를 제공하고, 프로세스 제어 및 관리를 위해 사용된다.

이를 통해 Node.js 애플리케이션 내부에서 프로세스 상태를 모니터링하고 제어할 수 있다.

 

예시

// 현재 프로세스의 아규먼트 출력
console.log(process.argv);

// 환경 변수 출력
console.log(process.env);

// 프로세스 종료
process.exit(0);

 

728x90
반응형

'Back-end & Server > Node.js' 카테고리의 다른 글

[Node.js] Event Loop  (0) 2023.07.29
[Node.js] Callback Function  (0) 2023.07.29
[Node.js] index.js 파해쳐보기  (0) 2023.07.29
[Node.js] package.json 알아보기  (0) 2023.07.28
[Node.js] Node.js 시작하기  (0) 2023.07.28