블로그 이미지
윤영식
Full Stacker, Application Architecter, KnowHow Dispenser and Bike Rider

Publication

Category

Recent Post

2012. 12. 10. 16:59 Lean Agile Culture/Architecturing

SlideShare 링크 



posted by 윤영식
2012. 12. 10. 16:35 NodeJS/Concept

node.js에서 File I/O 를 위하여 역시 모듈을 로딩하여 사용한다 



> require('fs') 포함 
> 비동기 방식은 마지막 파라미터로 완료 콜백함수를 받고, 첫 아규먼트로 에러를 받음. 성공이면 null 또는 undefined 가 된다 
> rename과 stat가 비동기 호출이므로 오류 발생가능성 존재 

fs.rename('/tmp/hello', '/tmp/world', function (err) {

  if (err) throw err;

  console.log('renamed complete');

});

fs.stat('/tmp/world', function (err, stats) {

  if (err) throw err;

  console.log('stats: ' + JSON.stringify(stats));

});

> 이를 해결하기 위하여 callback chain을 사용한다. 즉, callback안에 callback function을 둔다 

fs.rename('/tmp/hello', '/tmp/world', function (err) {

  if (err) throw err;

  fs.stat('/tmp/world', function (err, stats) {

    if (err) throw err;

    console.log('stats: ' + JSON.stringify(stats));

  });

});


-- 결과 내역 

stats: {"dev":0,"mode":33206,"nlink":1,"uid":0,"gid":0,"rdev":0,"ino":0,"size":1876992,"atime":"2012-09-17T18:05:45.000Z","mtime":"2012-09-17T18:05:48.000Z","ctime":"2012-09-17T18:05:41.000Z"}


'NodeJS > Concept' 카테고리의 다른 글

[EJS] 사용하기  (0) 2012.12.15
[Node.js] Global Objects  (0) 2012.12.10
[Node.js] EventEmitter 에 대하여  (0) 2012.12.10
[Node.js] debugger 사용하기  (0) 2012.12.10
[Express] Express Framework 사용하기  (0) 2012.10.30
posted by 윤영식
2012. 12. 10. 16:08 NodeJS/Concept

EventEmitter는 말 그대로 이벤트를 발생시키는 모든 객체를 지칭한다. 서버에 접속(connect)할 때 이벤트가 발생하고 파일을 읽을 때 이벤트가 발생하는 식으로 이미 내장된 이벤트들이 존재할 것이고, 사용자 정의 이벤트를 만들 수도 있을 것이다. 



> require('events') 첨부한다 
> 이벤트를 발생시키는 모든 객체는 events.EventEmitter 의 인스턴스 
> 이벤트 발생할 때 실행할 함수를 객체에 연결, 이러한 함수를 리스너(Listener)라고 부른다 
> EventEmitter 접근 : require('events').EventEmitter
> emitter.on(event, listener) 또는 emitter.addListener(event, listener)
  예) server.on('connection', function(stream){...});
> emitter.once(event, listener) : 한번만 수행 즉, 일회성 listener 
> emitter.removeListener(event, lisetener) : listener 제거 
> emitter.removeAllListeners() : 모든 listener 제거 
> emitter.setMaxListeners(n) : listener등록이 10개 default로 제한, emitter의 listener 갯수를 늘릴 수 있다. 


'NodeJS > Concept' 카테고리의 다른 글

[EJS] 사용하기  (0) 2012.12.15
[Node.js] Global Objects  (0) 2012.12.10
[Node.js] File I/O 사용하기  (0) 2012.12.10
[Node.js] debugger 사용하기  (0) 2012.12.10
[Express] Express Framework 사용하기  (0) 2012.10.30
posted by 윤영식
2012. 12. 10. 15:42 NodeJS/Concept

node명령으로 자바스크립트 코드를 수행할 때 콘솔상에서 debugging을 할 수 있다. 


  • debugger 키워드를 코드에 끼워 넣는다 : breakpoint 역할을 한다 
  • cont, c : 계속 진행 
  • next, n : step next
  • step, s : step in
  • out,   o : step out
  • pause  : 실행 멈춤 
  • 상세명령들

> 예제 코드 (debuggerTest.js)
x = 5;
setTimeout(function () {
  debugger;
  console.log("world");
}, 1000);
console.log("hello");



> 디버깅 수행 : node debug <수행 자바스크립트 파일명>

D:\Framework\Node.js> node debug debuggerTest

< debugger listening on port 5858

connecting... ok

debug> cont

< hello

break in D:\Framework\Node.js\debuggerTest.js:3

  1 x = 5;

  2 setTimeout(function () {

  3   debugger;

  4   console.log("world");

  5 }, 1000);

debug> next

break in D:\Framework\Node.js\debuggerTest.js:4

  2 setTimeout(function () {

  3   debugger;

  4   console.log("world");

  5 }, 1000);

  6 console.log("hello");

debug> cont

< world

program terminated


hello을 찍고 debugger가 있는 곳에서 멈추면 next step으로 이동하면 console 객체로 가고 cont하면 수행을 다시 하여 world 문자가 찍힌다. 수행되는 process를 command line에서 추적해 보고자 할 경우 사용하면 좋겠다. 


'NodeJS > Concept' 카테고리의 다른 글

[EJS] 사용하기  (0) 2012.12.15
[Node.js] Global Objects  (0) 2012.12.10
[Node.js] File I/O 사용하기  (0) 2012.12.10
[Node.js] EventEmitter 에 대하여  (0) 2012.12.10
[Express] Express Framework 사용하기  (0) 2012.10.30
posted by 윤영식
2012. 12. 8. 15:16 NodeJS

Node.js 주말강좌 요약하기 


> 참조 : 라이언 일병(Ryan Dahl) 의 발표와 PPT자료 : Node.js 에 대한 문화적, 구조적 차이를 이해하자!

> 참조 : Node.js API 사용 (예제 포함)


  • 현재 System(I/O) : L2 Cache <-> Memory <-> Disk  대비 Network I/O = compiler 서비스
  • Web System : a = b() 식의 assign 즉 sync를 사용하지 않는다. b(a) {} 를 사용하자 (async 방식)
  • b(a) {} 구문에서 Node.js는 a = call back 객체이다 = anonymous object = closure object = 스트림에서 이해됨 
  • stream = functional의 목적은 네트워크에 있는 모듈을 Merge하여 사용하는 것이다 = social web service 세대 
  • Node.js는 모듈 기반이고 모듈을 위한 스펙 CommandJS와 AMD를 이해하자 
  • Node.js의 수많은 Modules in GitHub
  • Node.js에 대한 기본적이 이해


'NodeJS' 카테고리의 다른 글

[Node.js] 프론트앤드 개발을 위한 Command-Line Tools (CLI)  (0) 2013.01.24
[Node.js] 역사와 기초  (0) 2012.12.08
[Node.js] 시작하기  (1) 2012.10.30
posted by 윤영식