1 / 23

Global Variables (1 / 2)

Global Variables (1 / 2). ㅇ [ Debug As] - [Node Application ]. ㅇ 코드 실행 시 개발자가 작성한 코드를 function 으로 묶은 후 총 5 개의 파라미터를 넘겨주면서 실행. - exports / require / module  node.js 내부 객체를 다루는 객체로 활용 - __filename / __ dirname  파일 이름 , 프로젝트 경로 등의 자료로 활용. Global Variables (2 / 2). ㅇ 코드 실행 결과.

merlin
Download Presentation

Global Variables (1 / 2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Global Variables (1 / 2) ㅇ[Debug As] - [Node Application] ㅇ 코드 실행 시 개발자가 작성한 코드를 function으로 묶은 후 총 5개의 파라미터를 넘겨주면서 실행 - exports / require / module  node.js 내부 객체를 다루는 객체로 활용 - __filename / __dirname 파일 이름, 프로젝트 경로 등의 자료로 활용

  2. Global Variables (2 / 2) ㅇ코드 실행 결과 console.log(__filename); console.log(__dirname);

  3. process 객체 (1 / 2) ㅇnode.js 에서만 지원되는 객체로써 여러가지 기본 정보를 가지고 있고 기본 기능들을 제공 console.log(process);

  4. process 객체 (2 / 2) ㅇ메소드 및 속성 console.log(process.execPath); console.log(process.cwd()); console.log(process.version); console.log(process.memoryUsage()); console.log(process.env);

  5. exports 객체 (1 / 2) ㅇ모듈 로딩 시스템 ㅇ 개발자가 직접 객체를 구현하고 exports 객체를 이용하여 재사용 exports.js varcal = require('./cal.js'); console.log(cal.sum(5, 6)); cal.js exports.sum = function(x, y) { x = parseInt(x); y = parseInt(y); return x + y; }

  6. exports 객체 (2 / 2) ㅇRequire를 이용하여 모듈 로딩시 같은 디렉토리에 있을 경우 ‘./’를 써주고 이외의 경우는 상대 / 절대 경로로 표기 ㅇ경로를 나타내지 않고 require(‘cal.js’)로 호출시에는node_modules디렉토리 생성 후 파일 추가 exports.js varcal = require('cal.js'); console.log(cal.sum(5, 6));

  7. Events (1 / 5) ㅇEvents 등록 - 자바스크립트에서는 addEventListener함수를 이용하여 click, mousemove등 다양한 이벤트를 등록하고 해당 이벤트 핸들러를 구현하여 사용 - node.js 에서는 EventEmitter클래스에 구현된 addListener와 on 두개의 함수를 이용하여 이벤트를 등록하여 사용 events1.js vareventHandler = function() { console.log('EXIT'); }; process.addListener('exit', eventHandler); // process.on(‘exit’, eventHandler);

  8. Events (2 / 5) ㅇEvents 삭제 events2.js process.on('exit', function(e) { console.log('EXIT'); }); var errHandler1 = function(e) { console.log('err1', e); }; var errHandler2 = function(e) { console.log('err2', e); }; process.on('uncaughtException', errHandler1); process.on('uncaughtException', errHandler2); process.removeListener('uncaughtException', errHandler2); error

  9. Events (3 / 5) ㅇEvents 발생 events3.js process.on('test', function() { console.log('Test Event!'); }); process.emit('test'); - emit() 메소드를 이용하여 이벤트 강제 발생

  10. Events (4 / 5) ㅇEvents 발생 (1 / 2) events4.js vareObj = require('eventObj.js'); eObj.obj.on('test', function() { console.log('test event'); }); console.log(eObj.obj.sum(5, 6)); eObj.obj.emit('test');

  11. Events (5 / 5) ㅇEvents 발생 (2 / 2) eventObj.js exports.obj = new process.EventEmitter(); exports.obj.sum = function(x, y) { return x + y; }; - 새로운객체에서 이벤트를 사용하기 위해 EventEmiter상속

  12. os모듈 (1 / 2) ㅇ 서버의 기본적인 하드웨어 자원들의 정보를 확인 os.js var os = require('os'); console.log(os.platform());

  13. os모듈 (2 / 2)

  14. File System 모듈 (1 / 4) ㅇ파일 핸들링 관련 메소드제공 ㅇ동기 / 비동기두가지 방식으로 제공 ㅇ 동기 방식의 경우, 비동기 방식 메소드에‘~Sync’라는 접미사를 붙임 ㅇ 파일 읽기 file1.js var fs = require('fs'); fs.readFile('../README.md', 'utf8', function(err, data) { if(err) { throw err; } console.log(data); }); 동기 방식  fs.readFileSync(‘../README.md’, ‘utf8’);

  15. File System 모듈 (2 / 4) ㅇ 파일 확인 file2.js var fs = require('fs'); // async fs.exists('../README2.md', function(exists) { console.log("async : " + exists); }); // sync var exists = fs.existsSync('../README.md'); console.log("sync : " + exists);

  16. File System 모듈 (3 / 4) ㅇ 파일 쓰기 file3.js var fs = require('fs'); // async fs.writeFile('../save1.txt', 'Hello Node', 'utf8', function(err) { if(err) { throw err; } console.log("async saved"); }); // sync fs.writeFile('../save2.txt', 'Hello Node', 'utf8');

  17. File System 모듈 (4 / 4) ㅇ 파일 삭제 file4.js var fs = require('fs'); // async fs.unlink('../save1.txt', function(err) { if(err) { throw err; } console.log("delete"); }); // sync fs.unlinkSync('../save2.txt');

  18. url모듈 ㅇUrl String 객체 ㅇ객체화 : url.parse() ㅇ직렬화 : url.format() url.js var url = require('url'); var obj = url.parse('https://www.google.co.kr/webhp?' + 'sourceid=chrome-instant&ion=1&' + 'espv=2&ie=UTF-8#newwindow=1&q=node.js'); console.log('url to Object : ', obj); console.log('============================='); console.log('Object to url : ', url.format(obj));

  19. util모듈 ㅇ 문자열 재구성 : util.format() ㅇ로그 출력 : util.log() util1.js var util = require('util'); var data = util.format('%s, %d, %j', 'foo', 10, {name:'node.js'}); console.log(data); - %s : 스트링, %d : 숫자, %j : JSON util2.js var util = require('util'); util.log('LOG TEST');

  20. net 모듈 (1 / 4) ㅇ 내부적으로 매우 많이 사용되지만, 일반적인 node.js 애플리케이션 개발에서는 거의 사용되지 않음 ㅇ express 같은 서버 역할을 해주는 외부 모듈에서 이미 구현되어 있기 때문에 개발자가 직접 net 모듈을 사용하여 개발하지 않음 net.js var net = require('net'); var server = net.createServer(function(c) { console.log('server connected'); c.on('end', function() { console.log('server disconnected'); }); c.write('hello\r\nThis is Telnet Service\r\n'); c.pipe(c); }); server.listen(8124, function() { console.log('server bound'); });

  21. net 모듈 (2 / 4) ㅇputty 접속 (1 / 2)

  22. net 모듈 (3 / 4) ㅇputty 접속 (2 / 2)

  23. net 모듈 (4 / 4) ㅇconsole 로그

More Related