Web DevelopmentDev.to
December 30, 20252 min read0 views
Share:

Node.js 기초 - 실행, 인자, 로그, 이벤트, 종료

Node.js의 기본적인 사용법에 대해 알아봅니다. node test.js process.argv.forEach((val, index) => { console.log(`${index}: ${val}`); }); 결과: node chrome.js aa 0: /usr/local/bin/node 1: /path/to/chrome.js 2: aa console.log('This example is different!'); console.l

Node.js의 기본적인 사용법에 대해 알아봅니다.

로컬 실행

node test.js

명령줄 인자 (Arguments)

process.argv.forEach((val, index) => {
    console.log(`${index}: ${val}`);
});

결과:

node chrome.js aa

0: /usr/local/bin/node 1: /path/to/chrome.js 2: aa

로그 출력

console.log('This example is different!');
console.log("Example app listening at http://%s:%s", host, port);

이벤트 (Observer 패턴)

var events = require('events');
var eventEmitter = new events.EventEmitter();

// 이벤트 핸들러 생성
var myEventHandler = function () {
    console.log('I hear a scream!');
}

// 이벤트에 핸들러 할당
eventEmitter.on('scream', myEventHandler);

// 'scream' 이벤트 발생
eventEmitter.emit('scream');

프로세스 종료

process.exit([code])

Originally published at https://dss99911.github.io

Originally published on Dev.to

Read Original Article

Subscribe to Our Newsletter

Get the latest tech news, insights, and updates delivered straight to your inbox. Join our community of tech enthusiasts!

We respect your privacy. Unsubscribe at any time. Unsubscribe here