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

Publication

Category

Recent Post

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 윤영식