21
Hello, Node.js Chulju, Hong PoolC

Node.js intro

Embed Size (px)

DESCRIPTION

Node.js 포교용

Citation preview

Page 1: Node.js intro

Hello, Node.js Chulju, Hong

PoolC

Page 2: Node.js intro

Node.js “소개” 일뿐 뭔가 끄적이고 싶다면

Try it yourself 저도 잘 몰라요 포교할 뿐

Page 3: Node.js intro

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, s c a l a b l e n e t w o r k applications.

단순한 웹 프레임워크 같은게 아니다!

스케일러블한 네트워크 어플리케이션! 강력해보인다!

Page 4: Node.js intro

Q : Node.js 가 뭔지 아세요?

A : django 같이 웹 사이트 만들 때 쓰는거 아니에요?

웹 프레임워크와는 다르다! 웹 프레임워크와는!

흔한 오해

앞에서도 썼지만

물론 만들 수는 있다  

Page 5: Node.js intro

Node.js 를 이용해서

Command Line Application Web Server TCP Server Bot 왠만한 (I/O가 많은 것이라도) Server

등을 만들 수 있답니다

Page 6: Node.js intro

굳이 Node.js를 써서 만들어야하나?

•  Frontend와 Backend가 javascript로 대동단결!  

• 강력한 V8 엔진 기반

•  Event-driven, non-blocking I/O

• 다른 걸 가져다 쓰거나 자신의 모듈을 게시하기 매우 쉬운 시스템

크롬에 쓰는 그거

날 가져요 npm 엉엉

Page 7: Node.js intro

Node.js 시작하기

Click!  

http://nodejs.org

Page 8: Node.js intro

길었다, 서론 본다! Hello, World!

console.log(“Hello, World!”);

웹 프로그래밍 하셨으면 다들 아시잖아요 익숙한 자바스크립트

하지만 IE에서 하면 어떻게 될까

Page 9: Node.js intro

길었다, 서론 본다! Hello, World!

그 코드를 hello.js 로 저장하고

Command Line에

node hello.js

그리고 Hello, World! 가 출력된다

참 쉽죠?

Page 10: Node.js intro

HTTP Server 만들기 Method 1 : 직접 만든다 (Node.js http 모듈을 이용)

var http = require('http'); var server = http.createServer( function(req, res) {

var body = 'Hello, World!'; res.writeHead(200, { 'Content-Length': body.length, 'Content-Type': 'text/plain' }); res.end(body);

} ); server.listen(3000);

Page 11: Node.js intro

HTTP Server 만들기 Method 2 : 만들어진 것들을 가져다쓴다 (Express, etc.)

가져다쓰는게 편합니다

ex)  express  

$  npm  install  -­‐g  express  

global

$  express  myapp  

$  cd  myapp    &&  npm  install  

$  node  app  

Page 12: Node.js intro

Node 모듈을 시작하는 방법 드디어 등장하는 npm

> npm init // package.json 을 생성함

{      "name":  "testapp",      "version":  "0.0.0",      "descrip@on":  "ERROR:  No  README.md  file  found!",      "main":  "index.js",      "scripts":  {          "test":  "echo  \"Error:  no  test  specified\"  &&  exit  1"      },      "repository":  "",      "author":  "",      "license":  "BSD"  }  

Page 13: Node.js intro

모듈 가져다쓰기

> 필요한 모듈 찾기

Google  it  :  node  {what  you  want}  

hTps://npmjs.org/    

> 모듈 깔기

npm  install  {module  name}  {-­‐-­‐save}  

> 앱에 모듈 가져오기

var  module  =  require(‘module  name’)  

Page 14: Node.js intro

적절한 모듈들

express,  socket.io,  mongoose,  phantom.js,  etc.  

몇 가지 안 써보긴 했지만 여기도 참고

웹 프레임워크

웹 소켓 흉내

MongoDB for Node.js

Full web stack No browser required

Page 15: Node.js intro

잉여 예제 - 콘솔에 colored text 출력하기

> 적절한 구글링을 통해 colors 라는 모듈을 발견

> 모듈 페이지로 가서 사용법을 확인

보통 Github등의 repository의 readme에 있음

> 자기 앱 디렉터리에서 npm install colors를 입력

var colors = require(‘colors’); console.log(‘Hello, World!’.rainbow);

모듈 없이 하는 것에 대해서는 이 곳을 참조

참 쉽죠?

Page 16: Node.js intro

모듈 만들기 > 모듈 작성하기

func@on  foo()  {    //blahblah  

}  

> 모듈 내보내기

exports.foo  =  foo;  

> 내 모듈 가져오기

var  my_module  =  require(‘./mymodule’);  mymodule.foo();  

위의 내용이 같은 폴더의 mymodule.js에 저장되어 있을 때

Page 17: Node.js intro

비동기 프로그래밍

기존에 써왔던 방식

int val = getValueSync(); 왜 C++?

익숙해져야할 방식

getValueAsync(function(val) { console.log(val);

});

콜백으로 값을 받아내서 이용

동기 다른 패러다임

Page 18: Node.js intro

비동기 프로그래밍 을 접한 사람들의 실수?

int val1 = getValue1(); int val2 = getValue2(); … int val100 = getValue100(); print(val1 + val2);

동기 -> 비동기로의 이식

function getValue1( function(val) { var val1 = val; function getValue2( function(val) { var val2 = val; … …

} ); } );

Page 19: Node.js intro

hTp://callbackhell.com/    

여기 들어가봅시다

이름만 봐도 헬

Page 20: Node.js intro

자잘한 팁 node.js 웹 어플리케이션을 작성할 때,

코드 수정 -> 앱 재시작이 너무 귀찮아요

supervisor 라는 모듈을 사용해봅시다.

(https://github.com/isaacs/node-supervisor)

javascript 쓰기 싫어요

뭐라할 말은 없는데 coffeescript, dart 같은걸 끼얹어본다던가..

Opal(http://opalrb.org/) 같은 것도 있어요 (Ruby to javascript compiler)

Page 21: Node.js intro

Any  Ques@ons?