31
Node.js 시작하기 ABC Talk / Steve Ahn

Node.js 시작하기

Embed Size (px)

DESCRIPTION

ABC Talk #12 Node.js 시작하기 실습 예제: https://github.com/ohgyun/nsrank/

Citation preview

Page 1: Node.js 시작하기

Node.js����������� ������������������  시작하기

ABC����������� ������������������  Talk����������� ������������������  /����������� ������������������  Steve����������� ������������������  Ahn

Page 2: Node.js 시작하기

오늘은,

-����������� ������������������  Node.js의����������� ������������������  특징을����������� ������������������  아주����������� ������������������  간단히����������� ������������������  살펴보고,

-����������� ������������������  개발을����������� ������������������  시작할����������� ������������������  때����������� ������������������  필요한����������� ������������������  몇����������� ������������������  가지����������� ������������������  개념����������� ������������������  위주로,

-����������� ������������������  작은����������� ������������������  앱을����������� ������������������  만들어볼����������� ������������������  예정이예요.

-����������� ������������������  노드로����������� ������������������  무언가를����������� ������������������  만들����������� ������������������  수����������� ������������������  있는����������� ������������������  발판을����������� ������������������  마련하는����������� ������������������  것이����������� ������������������  목적!

Page 3: Node.js 시작하기

목차

-����������� ������������������  Node.js란?

-����������� ������������������  모듈����������� ������������������  정의

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  -����������� ������������������  require,����������� ������������������  module,����������� ������������������  exports,����������� ������������������  CommonJS

-����������� ������������������  노드����������� ������������������  패키지����������� ������������������  매니저

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  -����������� ������������������  npm,����������� ������������������  package.json,����������� ������������������  node_modules

-����������� ������������������  실습

-����������� ������������������  더����������� ������������������  알아보기

Page 4: Node.js 시작하기

Node.js란?

Node.js����������� ������������������  is����������� ������������������  a����������� ������������������  platform����������� ������������������  built����������� ������������������  on����������� ������������������  Chrome's����������� ������������������  JavaScript����������� ������������������  runtime

for����������� ������������������  easily����������� ������������������  building����������� ������������������  fast,����������� ������������������  scalable����������� ������������������  network����������� ������������������  applications.

Node.js����������� ������������������  uses����������� ������������������  an����������� ������������������  event-driven,����������� ������������������  non-blocking����������� ������������������  I/O����������� ������������������  model

that����������� ������������������  makes����������� ������������������  it����������� ������������������  lightweight����������� ������������������  and����������� ������������������  efficient,

perfect����������� ������������������  for����������� ������������������  data-intensive����������� ������������������  real-time����������� ������������������  applications����������� ������������������  that����������� ������������������  run����������� ������������������  

across����������� ������������������  distributed����������� ������������������  devices.����������� ������������������  

Page 5: Node.js 시작하기

Node.js란?

Node.js����������� ������������������  is����������� ������������������  a����������� ������������������  platform����������� ������������������  built����������� ������������������  on����������� ������������������  Chrome's����������� ������������������  JavaScript����������� ������������������  runtime

for����������� ������������������  easily����������� ������������������  building����������� ������������������  fast,����������� ������������������  scalable����������� ������������������  network����������� ������������������  applications.

Node.js����������� ������������������  uses����������� ������������������  an����������� ������������������  event-driven,����������� ������������������  non-blocking����������� ������������������  I/O����������� ������������������  model

that����������� ������������������  makes����������� ������������������  it����������� ������������������  lightweight����������� ������������������  and����������� ������������������  efficient,

perfect����������� ������������������  for����������� ������������������  data-intensive����������� ������������������  real-time����������� ������������������  applications����������� ������������������  that����������� ������������������  run����������� ������������������  

across����������� ������������������  distributed����������� ������������������  devices.����������� ������������������  

Page 6: Node.js 시작하기

Node.js란?

Node.js����������� ������������������  is����������� ������������������  a����������� ������������������  platform����������� ������������������  built����������� ������������������  on����������� ������������������  Chrome's����������� ������������������  JavaScript����������� ������������������  runtime

for����������� ������������������  easily����������� ������������������  building����������� ������������������  fast,����������� ������������������  scalable����������� ������������������  network����������� ������������������  applications.

Node.js����������� ������������������  uses����������� ������������������  an����������� ������������������  event-driven,����������� ������������������  non-blocking����������� ������������������  I/O����������� ������������������  model

that����������� ������������������  makes����������� ������������������  it����������� ������������������  lightweight����������� ������������������  and����������� ������������������  efficient,

perfect����������� ������������������  for����������� ������������������  data-intensive����������� ������������������  real-time����������� ������������������  applications����������� ������������������  that����������� ������������������  run����������� ������������������  

across����������� ������������������  distributed����������� ������������������  devices.����������� ������������������  

Page 7: Node.js 시작하기

Node.js란?

Node.js����������� ������������������  is����������� ������������������  a����������� ������������������  platform����������� ������������������  built����������� ������������������  on����������� ������������������  Chrome's����������� ������������������  JavaScript����������� ������������������  runtime

for����������� ������������������  easily����������� ������������������  building����������� ������������������  fast,����������� ������������������  scalable����������� ������������������  network����������� ������������������  applications.

Node.js����������� ������������������  uses����������� ������������������  an����������� ������������������  event-driven,����������� ������������������  non-blocking����������� ������������������  I/O����������� ������������������  model

that����������� ������������������  makes����������� ������������������  it����������� ������������������  lightweight����������� ������������������  and����������� ������������������  efficient,

perfect����������� ������������������  for����������� ������������������  data-intensive����������� ������������������  real-time����������� ������������������  applications����������� ������������������  that����������� ������������������  run����������� ������������������  

across����������� ������������������  distributed����������� ������������������  devices.����������� ������������������  

Page 8: Node.js 시작하기

흔한����������� ������������������  HTTP����������� ������������������  서버����������� ������������������  예제

<example.js>

var����������� ������������������  http����������� ������������������  =����������� ������������������  require('http');

http.createServer(function����������� ������������������  (req,����������� ������������������  res)����������� ������������������  {

����������� ������������������  ����������� ������������������  res.writeHead(200,����������� ������������������  {'Content-Type':����������� ������������������  'text/plain'});

����������� ������������������  ����������� ������������������  res.end('Hello����������� ������������������  World\n');

}).listen(8000,����������� ������������������  '127.0.0.1');

console.log('Server����������� ������������������  running����������� ������������������  at����������� ������������������  http://127.0.0.1:8000/');

Page 9: Node.js 시작하기

흔한����������� ������������������  HTTP����������� ������������������  서버����������� ������������������  예제

$����������� ������������������  node����������� ������������������  example.js

Server����������� ������������������  running����������� ������������������  at����������� ������������������  http://127.0.0.1:8000/

Page 10: Node.js 시작하기

좀����������� ������������������  더����������� ������������������  자세한����������� ������������������  내용은?

node.js

Page 11: Node.js 시작하기

실용적인����������� ������������������  것!

-����������� ������������������  정의나����������� ������������������  특성은����������� ������������������  검색하면����������� ������������������  쉽게����������� ������������������  찾을����������� ������������������  수����������� ������������������  있으니,

-����������� ������������������  오늘은����������� ������������������  실용적인����������� ������������������  것만����������� ������������������  보기로����������� ������������������  해요.

-����������� ������������������  모듈����������� ������������������  정의와����������� ������������������  패키지����������� ������������������  매니저에����������� ������������������  대해서만!

Page 12: Node.js 시작하기

모듈����������� ������������������  정의하기

-����������� ������������������  하나의����������� ������������������  파일은����������� ������������������  하나의����������� ������������������  모듈.����������� ������������������  파일명이����������� ������������������  모듈����������� ������������������  이름

-����������� ������������������  `.js`����������� ������������������  확장자를����������� ������������������  사용

-����������� ������������������  모듈의����������� ������������������  API를����������� ������������������  외부로����������� ������������������  공개하려고����������� ������������������  할����������� ������������������  땐,

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  `exports`����������� ������������������  변수의����������� ������������������  속성으로����������� ������������������  할당

-����������� ������������������  생성자나����������� ������������������  전체����������� ������������������  객체를����������� ������������������  내보낼����������� ������������������  땐����������� ������������������  `module.exports`에����������� ������������������  할당

Page 13: Node.js 시작하기

모듈����������� ������������������  정의하기

<foo.js>

var����������� ������������������  myName����������� ������������������  =����������� ������������������  ‘foo’;

exports.whoami����������� ������������������  =����������� ������������������  function����������� ������������������  ()����������� ������������������  {

����������� ������������������  ����������� ������������������  return����������� ������������������  myName;

};

Page 14: Node.js 시작하기

모듈����������� ������������������  가져오기

require(모듈명����������� ������������������  또는����������� ������������������  경로)

require(‘http’);����������� ������������������  //-->����������� ������������������  코어����������� ������������������  모듈

require(‘./foo’);����������� ������������������  //-->����������� ������������������  동일한����������� ������������������  디렉토리의����������� ������������������  파일

require(‘foo’);����������� ������������������  //-->����������� ������������������  다른����������� ������������������  패키지의����������� ������������������  모듈

Page 15: Node.js 시작하기

모듈����������� ������������������  정의하기

<bar.js>

var����������� ������������������  foo����������� ������������������  =����������� ������������������  require(‘./foo.js’);

foo.whoami();����������� ������������������  //-->����������� ������������������  ‘foo’

Page 16: Node.js 시작하기

CommonJS����������� ������������������  /����������� ������������������  Modules

-����������� ������������������  CommonJS:����������� ������������������  자바스크립트����������� ������������������  생태계를����������� ������������������  위한����������� ������������������  API����������� ������������������  표준����������� ������������������  제정

-����������� ������������������  Modules����������� ������������������  API:����������� ������������������  모듈을����������� ������������������  정의하고����������� ������������������  가져오는����������� ������������������  것에����������� ������������������  대한����������� ������������������  API

-����������� ������������������  node.js,����������� ������������������  MongoDB,����������� ������������������  RingoJS����������� ������������������  등에서����������� ������������������  구현

-����������� ������������������  연관����������� ������������������  주제:����������� ������������������  AMD,����������� ������������������  RequireJS

Page 17: Node.js 시작하기

모듈����������� ������������������  로딩����������� ������������������  순서

1.����������� ������������������  코어����������� ������������������  모듈이면����������� ������������������  바로����������� ������������������  리턴

2.����������� ������������������  ‘./’,����������� ������������������  ‘/’,����������� ������������������  ‘../’����������� ������������������  등����������� ������������������  절대/상대����������� ������������������  경로로����������� ������������������  시작하면����������� ������������������  파일이나����������� ������������������  디렉토리����������� ������������������  로드

3.����������� ������������������  이름에����������� ������������������  해당하는����������� ������������������  외부����������� ������������������  모듈����������� ������������������  로드

4.����������� ������������������  못����������� ������������������  찾으면����������� ������������������  `not����������� ������������������  found`����������� ������������������  오류����������� ������������������  던짐

http://nodejs.org/api/modules.html#modules_all_together

Page 18: Node.js 시작하기

패키지����������� ������������������  관리

-����������� ������������������  NPM:����������� ������������������  Node����������� ������������������  Package����������� ������������������  Manager

-����������� ������������������  https://npmjs.org/

-����������� ������������������  노드����������� ������������������  인스톨에����������� ������������������  기본으로����������� ������������������  포함되어����������� ������������������  있음

$����������� ������������������  npm

Page 19: Node.js 시작하기

외부����������� ������������������  패키지����������� ������������������  추가하기

$����������� ������������������  npm����������� ������������������  install����������� ������������������  <packageName>����������� ������������������  #����������� ������������������  다른����������� ������������������  패키지����������� ������������������  설치

-����������� ������������������  설치한����������� ������������������  외부����������� ������������������  패키지는����������� ������������������  `node_modules`����������� ������������������  디렉토리에����������� ������������������  추가

Page 20: Node.js 시작하기

의존����������� ������������������  모듈����������� ������������������  관리하기

-����������� ������������������  프로젝트의����������� ������������������  의존����������� ������������������  모듈을����������� ������������������  관리하고자����������� ������������������  한다면?

-����������� ������������������  내가����������� ������������������  만든����������� ������������������  패키지를����������� ������������������  배포하고����������� ������������������  싶다면?

-����������� ������������������  먼저,����������� ������������������  `package.json`을����������� ������������������  정의한다.

Page 21: Node.js 시작하기

package.json

-����������� ������������������  패키지의����������� ������������������  내용을����������� ������������������  정의하고����������� ������������������  있는����������� ������������������  매니페스트����������� ������������������  파일

-����������� ������������������  http://package.json.nodejitsu.com/

$����������� ������������������  npm����������� ������������������  init����������� ������������������  #����������� ������������������  package.json����������� ������������������  생성

Page 22: Node.js 시작하기

외부����������� ������������������  패키지����������� ������������������  추가하기

$����������� ������������������  npm����������� ������������������  install����������� ������������������  <packageName>����������� ������������������  --save����������� ������������������  #����������� ������������������  package.json에����������� ������������������  추가

$����������� ������������������  npm����������� ������������������  install����������� ������������������  <packageName>����������� ������������������  --save-dev����������� ������������������  #����������� ������������������  개발용

$����������� ������������������  npm����������� ������������������  install����������� ������������������  #����������� ������������������  `package.json`의����������� ������������������  정보로����������� ������������������  의존����������� ������������������  패키지����������� ������������������  설치

Page 23: Node.js 시작하기

실습

-����������� ������������������  네이버����������� ������������������  오픈����������� ������������������  API로����������� ������������������  커맨드라인에서����������� ������������������  실시간����������� ������������������  급상승어����������� ������������������  가져오기

-����������� ������������������  커맨드라인에서����������� ������������������  결과����������� ������������������  출력하기

-����������� ������������������  만든����������� ������������������  모듈을����������� ������������������  npm����������� ������������������  리파지터리로����������� ������������������  퍼블리시하기

Page 24: Node.js 시작하기

실습

$����������� ������������������  npm����������� ������������������  install����������� ������������������  nsrank

require(‘nsrank’).get(function����������� ������������������  (ranks)����������� ������������������  {

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  console.log(ranks);

});

Page 25: Node.js 시작하기

실습

$����������� ������������������  nsrank

1����������� ������������������  김동현

2����������� ������������������  맥도날드����������� ������������������  할머니

3����������� ������������������  김동현����������� ������������������  에릭실바

4����������� ������������������  조선이공대

Page 26: Node.js 시작하기

실습

url.txtAPI����������� ������������������  URL����������� ������������������  정보

apiUrlReader.jsURL����������� ������������������  읽어오기

nsrank.js랭크����������� ������������������  가져오기

cli.js커맨드라인����������� ������������������  출력

Page 27: Node.js 시작하기

npm에����������� ������������������  등록하기

-����������� ������������������  npm����������� ������������������  계정����������� ������������������  생성:����������� ������������������  https://npmjs.org/signup

-����������� ������������������  커맨드라인에서����������� ������������������  로그인

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  $����������� ������������������  npm����������� ������������������  login

-����������� ������������������  패키지를����������� ������������������  완성한����������� ������������������  디렉토리에서����������� ������������������  퍼블리시(업데이트도����������� ������������������  동일)

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  $����������� ������������������  npm����������� ������������������  publish

Page 28: Node.js 시작하기

Q&A

Page 29: Node.js 시작하기

더����������� ������������������  알아보기(대표적인����������� ������������������  외부����������� ������������������  모듈)

모듈����������� ������������������  검색은����������� ������������������  `npmjs.org`나����������� ������������������  구글에서~

-����������� ������������������  express:����������� ������������������  웹����������� ������������������  애플리케이션����������� ������������������  프레임워크

-����������� ������������������  connect:����������� ������������������  HTTP����������� ������������������  미들웨어����������� ������������������  프레임워크

-����������� ������������������  socket.io:����������� ������������������  리얼타임����������� ������������������  앱����������� ������������������  지원을����������� ������������������  위한����������� ������������������  라이브러리

-����������� ������������������  grunt:����������� ������������������  빌드����������� ������������������  도구

Page 30: Node.js 시작하기

더����������� ������������������  알아보기(Callback����������� ������������������  Hell)

중첩����������� ������������������  콜백은����������� ������������������  어떻게����������� ������������������  해결할����������� ������������������  수����������� ������������������  있을까?

-����������� ������������������  중첩된����������� ������������������  코드를����������� ������������������  각각의����������� ������������������  함수로����������� ������������������  분리

-����������� ������������������  비동기����������� ������������������  처리를����������� ������������������  위한����������� ������������������  모듈을����������� ������������������  활용:����������� ������������������  async����������� ������������������  모듈

-����������� ������������������  프라미스����������� ������������������  패턴을����������� ������������������  활용:����������� ������������������  Promises����������� ������������������  Pattern,����������� ������������������  Q����������� ������������������  Library

-����������� ������������������  관련����������� ������������������  문서

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  -����������� ������������������  http://callbackhell.com/

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  -����������� ������������������  How����������� ������������������  to����������� ������������������  avoid����������� ������������������  long����������� ������������������  nesting����������� ������������������  of����������� ������������������  asynchronous����������� ������������������  functions����������� ������������������  in����������� ������������������  Node.js?

Page 31: Node.js 시작하기

감사합니다!