105
12년 6월 23일 토요일

[Frends] go together with nodejs

Embed Size (px)

DESCRIPTION

FRENDS (http://blog.frends.kr/) 6차모임때 발표한 Go together with node.js

Citation preview

Page 1: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 2: [Frends] go together with nodejs

Go together node.jsNode.js Happy Hacking

@nanhapark

12년 6월 23일 토요일

Page 3: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 4: [Frends] go together with nodejs

About Presentation.

12년 6월 23일 토요일

Page 5: [Frends] go together with nodejs

About Presentation.

취지Node.js 를 재미있게 표현하여 조금이나마 알리자는 목적으로 제작목차

Node.js 실무여정Node.js Black Edition && Nodeman 제작과정Node.js Module 컴파일 과정Node.js Module을 빌드시점에 Native Module로 Builtin 방법

12년 6월 23일 토요일

Page 6: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 7: [Frends] go together with nodejs

Node.js 실무여정

12년 6월 23일 토요일

Page 8: [Frends] go together with nodejs

Node.js 실무여정

미친거 아냐?

12년 6월 23일 토요일

Page 9: [Frends] go together with nodejs

Node.js 실무여정

미친거 아냐?

괜찮다. 이럴때 시작해보자

12년 6월 23일 토요일

Page 10: [Frends] go together with nodejs

Node.js 실무여정

팀장님에게 이번 프로젝트는 Node.js로 진행해 보겠습니다. m(_ _)m

미친거 아냐?

괜찮다. 이럴때 시작해보자

12년 6월 23일 토요일

Page 11: [Frends] go together with nodejs

Node.js 실무여정

팀장님에게 이번 프로젝트는 Node.js로 진행해 보겠습니다. m(_ _)m

미친거 아냐?

괜찮다. 이럴때 시작해보자

내가 총대를 매보자.

12년 6월 23일 토요일

Page 12: [Frends] go together with nodejs

Node.js 실무여정

팀장님에게 이번 프로젝트는 Node.js로 진행해 보겠습니다. m(_ _)m

미친거 아냐?

후덜덜

괜찮다. 이럴때 시작해보자

내가 총대를 매보자.

12년 6월 23일 토요일

Page 13: [Frends] go together with nodejs

Node.js 실무여정

팀장님에게 이번 프로젝트는 Node.js로 진행해 보겠습니다. m(_ _)m

미친거 아냐?

후덜덜

괜찮다. 이럴때 시작해보자

내가 총대를 매보자.

대한민국에서 최고가

되보자

12년 6월 23일 토요일

Page 14: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 15: [Frends] go together with nodejs

Node.js 장단점

12년 6월 23일 토요일

Page 16: [Frends] go together with nodejs

Node.js 장단점

장점

Memory 11MB / 1 process

but, apache httpd 33MB / 1 process

Javascript API

단점

callback style (CPS)

Process를 띄우는 즉시 개발자 자신의 책임

12년 6월 23일 토요일

Page 17: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 18: [Frends] go together with nodejs

Node.js 실무작업 목록

12년 6월 23일 토요일

Page 19: [Frends] go together with nodejs

Node.js 실무작업 목록

그룹웨어 Chatting Server 프로젝트OAuth, Socket.io

HAProxy, Stunnel, Redis

CloudBuild 프로젝트API 서버, 아카이브 업로드서버, 인증키 업로드서버

ALS API (Sendmail) 프로젝트

기타 개인적인 홈페이지 nodeqa, naggomsu

12년 6월 23일 토요일

Page 20: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 21: [Frends] go together with nodejs

한번 해볼까?! 고고고고

12년 6월 23일 토요일

Page 22: [Frends] go together with nodejs

openapi 를 호출하여 무언가 해보자

작업시작 !! 유후~~ 재밌겠다.

음.. http native module이 있군.

var http = require(‘http’); 그 다음..

createClient, request, parameters, callbacks.....

한번 해볼까?! 고고고고

12년 6월 23일 토요일

Page 23: [Frends] go together with nodejs

openapi 를 호출하여 무언가 해보자

작업시작 !! 유후~~ 재밌겠다.

음.. http native module이 있군.

var http = require(‘http’); 그 다음..

createClient, request, parameters, callbacks.....

한번 해볼까?! 고고고고

12년 6월 23일 토요일

Page 24: [Frends] go together with nodejs

openapi 를 호출하여 무언가 해보자

작업시작 !! 유후~~ 재밌겠다.

음.. http native module이 있군.

var http = require(‘http’); 그 다음..

createClient, request, parameters, callbacks.....

한번 해볼까?! 고고고고

12년 6월 23일 토요일

Page 25: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 26: [Frends] go together with nodejs

작업 과정에서 느꼈던 아쉬운 점들

12년 6월 23일 토요일

Page 27: [Frends] go together with nodejs

작업 과정에서 느꼈던 아쉬운 점들

TIP 공유 커뮤니티 부족

인터페이스 수준의 API 제공

제공되는 API를 사용하여 작업하기 어렵다.

원하는 결과를 얻기 위해 적지 않은 코딩을 해야한다.

Meta Programming 시대에 왠말인가.....

해결방법

신들린 구글링 -> 시간소요 ;; project deadline ....;;

3rd party module name을 알아야 하고, github 소스를 분석 / 삽질 시작

12년 6월 23일 토요일

Page 28: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 29: [Frends] go together with nodejs

3rd party module keyword 중요성 증대

12년 6월 23일 토요일

Page 30: [Frends] go together with nodejs

3rd party module keyword 중요성 증대

제공되는 API를 이용하여 작업하는데 소비하는 비용이 크다.

restler or request 라는 키워드만 알고 있어도 확실한 도움이 될 것이다.

하지만, 키워드를 알아도 사용방법을 브라우져를 열고 검색해서 알아내야 하고, 대중적인 사용방법 또한 익혀야 한다.

더 빠르고, 직관된 검색을 유도하자. 즉, linux man 과 같은것을 만들어보자.

12년 6월 23일 토요일

Page 31: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 32: [Frends] go together with nodejs

자 그럼 3rd party module 찾아서...

12년 6월 23일 토요일

Page 33: [Frends] go together with nodejs

자 그럼 3rd party module 찾아서...

J u n g l e

12년 6월 23일 토요일

Page 34: [Frends] go together with nodejs

자 그럼 3rd party module 찾아서...

12년 6월 23일 토요일

Page 35: [Frends] go together with nodejs

자 그럼 3rd party module 찾아서...

File System - byline | fileutils | glob | graceful-fs | minimatch | mkdirp |

Control Flow - async | fibers | fibrous | step |

jsdom, request, restler, mongoose, node-static, ejs, jade, xml2js, js2xml, socket.io, commander, optimist, uuid, underscore, moment, emailjs, express, jade, mocha, ejs

etc ...., 너무 많다...... 같은 종류의 모듈이 많고, 어떤 모듈이 대중화된지 모르겠다.

12년 6월 23일 토요일

Page 36: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 37: [Frends] go together with nodejs

Node.js Black Edition&& Nodeman Project Start

12년 6월 23일 토요일

Page 38: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 39: [Frends] go together with nodejs

Roadmap

12년 6월 23일 토요일

Page 40: [Frends] go together with nodejs

Roadmap

유용하고 검증된 3rd party module을 Native Module으로 컴파일하자.

대중성 있는 Module이 무엇일까?

Most Depend on, http://search.npmjs.org/

실무에서 진행중인 프로젝트에 적용해보자

작업중에 모듈의 사용성에 불편함을 없애보자.

http://black.nodeman.org

http://nodeman.org12년 6월 23일 토요일

Page 41: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 42: [Frends] go together with nodejs

디렉토리 구성

12년 6월 23일 토요일

Page 43: [Frends] go together with nodejs

디렉토리 구성/benchmark/deps: opensources

/http_parser / openssl / libuv/v8/zlib

/doc: 문서/lib: 네이티브 모듈 (*. js)/out: 빌드 디렉토리/src: core + 기본 모듈 (node_ *. cc)/test: 테스트 스크립트/tools: 빌드 도구 (js2c)

12년 6월 23일 토요일

Page 44: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 45: [Frends] go together with nodejs

v8 util JS2C

12년 6월 23일 토요일

Page 46: [Frends] go together with nodejs

v8 util JS2C

Native Module (http, fs ..) 를 v8에 compile하기 위한 도구

Python, ./tools/js2c.py

WAF Build Script (wscript) 에서 import

Convert Javascript (lib/**) to C source (header)

src/node_javascript.cc 에서 header file load

./out/Release/src/node_natives.h

12년 6월 23일 토요일

Page 47: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 48: [Frends] go together with nodejs

JS2C Live Coding- console.js 컴파일 / 디컴파일 과정

12년 6월 23일 토요일

Page 49: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 50: [Frends] go together with nodejs

Native Module Require Step

12년 6월 23일 토요일

Page 51: [Frends] go together with nodejs

Native Module Require Step

default

javascript -> file load -> v8 compile

hacking

javascript -> file load -> c -> v8 compile

not npm

3rd party module 찾아서 여행을 떠나지 않아도 된다. 바로 require ! (이 기분 실무에서 정말 편함!!)

12년 6월 23일 토요일

Page 52: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 53: [Frends] go together with nodejs

Nodeman (Node.js Manual)

12년 6월 23일 토요일

Page 54: [Frends] go together with nodejs

Nodeman (Node.js Manual)

Node.js Black Edition Helper http://nodeman.orgnpm과 같이 설치시 빌트인 (wscript line 1013 포함)터미널에서 바로 Manual && Snippet code를 얻자.

Node.js 기본 API 를 비롯하여 유용한 3rd party module manual 제공해보자.약 50개의 대중적인 검증된 모듈 위주로 readme.md 내용을 COLORFUL 작업하여 집약시킴.

usage: nodeman [modulename]builtin list: nodeman -b카테고리별로 검증된 모듈목록을 확인할 수 있다.

기본 API 포함, 약 40개 Native Module 작업완료. update ... ing...

12년 6월 23일 토요일

Page 55: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 56: [Frends] go together with nodejs

Nodeman Live Coding- 기본 사용방법- with npm install - wscript line 1013

12년 6월 23일 토요일

Page 57: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 58: [Frends] go together with nodejs

Node.js Funny Structure.

12년 6월 23일 토요일

Page 59: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 60: [Frends] go together with nodejs

Node.js 언어 구성

12년 6월 23일 토요일

Page 61: [Frends] go together with nodejs

Node.js 언어 구성

Python: Build Script

Javascript: API (native module)

cpp: API, core (v8)

c: opensource (libuv, libev, libeio, http_parser, zlib ...)

12년 6월 23일 토요일

Page 62: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 63: [Frends] go together with nodejs

Event Driven

12년 6월 23일 토요일

Page 64: [Frends] go together with nodejs

Event Driven

12년 6월 23일 토요일

Page 65: [Frends] go together with nodejs

Event Driven

12년 6월 23일 토요일

Page 66: [Frends] go together with nodejs

Event Driven

12년 6월 23일 토요일

Page 67: [Frends] go together with nodejs

Event Driven

인생은 이벤트의 연속이다.휴;;

12년 6월 23일 토요일

Page 68: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 69: [Frends] go together with nodejs

Event Loop && Opensources

12년 6월 23일 토요일

Page 70: [Frends] go together with nodejs

Event Loop && Opensources

12년 6월 23일 토요일

Page 71: [Frends] go together with nodejs

Event Loop && Opensources

epoll/kqueue

당신은 이벤트를 달력에 등록하고

libeio

그것을 실행하는 사람은 당신 혼자일 수도 있고, 여러개의 이벤트를 각각의 이벤트업체나 친구들에게 맡겨서 이벤트를 진행시킬 수 있다.

libev, CPS Style

당신이 똑똑하고, 가정적이고, 실행능력이 뛰어나다면, 이벤트 실행은 별거 아니다.

12년 6월 23일 토요일

Page 72: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 73: [Frends] go together with nodejs

단일쓰레드 ? 웃기셔. 메인쓰레드만 단일 !

12년 6월 23일 토요일

Page 74: [Frends] go together with nodejs

단일쓰레드 ? 웃기셔. 메인쓰레드만 단일 !

I/O 처리libeio

12년 6월 23일 토요일

Page 75: [Frends] go together with nodejs

단일쓰레드 ? 웃기셔. 메인쓰레드만 단일 !

I/O 처리libeio

메인쓰레드의V8

(google chrome)

메인쓰레드만 단일쓰레드

file I/O

max polling count 10

12년 6월 23일 토요일

Page 76: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 77: [Frends] go together with nodejs

Architecture & I/O Flow (libeio)

12년 6월 23일 토요일

Page 78: [Frends] go together with nodejs

Architecture & I/O Flow (libeio)

12년 6월 23일 토요일

Page 79: [Frends] go together with nodejs

Architecture & I/O Flow (libeio)

http://nodeqa.com/nodejs_ref/612년 6월 23일 토요일

Page 80: [Frends] go together with nodejs

Architecture & I/O Flow (libeio)

http://nodeqa.com/nodejs_ref/612년 6월 23일 토요일

Page 81: [Frends] go together with nodejs

Architecture & I/O Flow (libeio)

http://nodeqa.com/nodejs_ref/612년 6월 23일 토요일

Page 82: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 83: [Frends] go together with nodejs

Module Internal Binding

12년 6월 23일 토요일

Page 84: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 85: [Frends] go together with nodejs

최초 $ Node 실행시 프롬프트 뜨기 전까지

12년 6월 23일 토요일

Page 86: [Frends] go together with nodejs

최초 $ Node 실행시 프롬프트 뜨기 전까지

제일 먼저 실행되는 파일 src/node_main.cc

int main....

두번째로 실행되는 파일 src/node.cc LINE 2873, 2310

process 객체 생성 ex) process.binding, process API

lib/**.js Native Modules v8 compile node_natives.h

src/node_javascript.cc LINE 36

event loop start

12년 6월 23일 토요일

Page 87: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 88: [Frends] go together with nodejs

Javascript를 Native Module로... #1

12년 6월 23일 토요일

Page 89: [Frends] go together with nodejs

Javascript를 Native Module로... #1

very easy !!lib 디렉토리에 넣으면 된다.

lib/**.js 파일들의 기본취지node.js API Programming

fs, http, console....cpp module 을 API로 제공하기 위한 Bridge

process.binding 으로 cpp 불러들임.

12년 6월 23일 토요일

Page 90: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 91: [Frends] go together with nodejs

Javascript를 Native Module로... #2

12년 6월 23일 토요일

Page 92: [Frends] go together with nodejs

Javascript를 Native Module로... #2

lib/**.js 생성하기

** 에 포함되는 dependency 존재할 시 **_sub.js 형태로 만들어서 require 하는것이 일반적. 서브디렉토리는 지원하지 않음. 예) require “winston_transport.js”

fs, http ... 등 모두 1개의 javascript 파일로 이루어져 있으며, process.binding 을 사용하여 cpp lib import

WAF build tool 에 의하여 변경사항이 있을경우 rebuild

make && make install

12년 6월 23일 토요일

Page 93: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 94: [Frends] go together with nodejs

process.binding()

12년 6월 23일 토요일

Page 95: [Frends] go together with nodejs

process.binding()

Node API 의 핵심역할

cpp module을 javascript에서 불러들일 수 있도록 중개역할을 한다.

FROM src/node_**.cc TO lib/**.js

ex) http (http_parser)

binding을 lib/**.js 에서 사용할 수 있도록 API 형식으로 선언

src/node.cc LINE 1727, 2101

NODE_SET_METHOD (process, "binding", Binding);

12년 6월 23일 토요일

Page 96: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 97: [Frends] go together with nodejs

Native Module을 global Module로...

12년 6월 23일 토요일

Page 98: [Frends] go together with nodejs

Native Module을 global Module로...

not Require

src/node.js line 47, 175

require 없이 사용하고 싶다.

javascript 를 사용하여 원하는 대로 hacking 가능하다.

12년 6월 23일 토요일

Page 99: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 100: [Frends] go together with nodejs

cpp를 native module로 만들어보자.

12년 6월 23일 토요일

Page 101: [Frends] go together with nodejs

cpp를 native module로 만들어보자.

일반적인 방법 http://nodejs.org/api/addons.html

*.node 만들기 && require

src/node_**.h (optinal), src/node_**.cc

src/node_extension.h 추가

lib/**.js 추가하고 process.binding 사용하여 import

wscript 918 LINE 부분에 추가

make && make install

12년 6월 23일 토요일

Page 102: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 103: [Frends] go together with nodejs

Native Module Live Coding- javascript - lib/*.js, nanhapark keyword - require- cpp - node_extend.cc[h], extend.js - vi src/node_extension.h +52 - vi wscript +922- make 과정 - 빌드파일 변경여부 판단 - find 하여 포함된 여부 - node -> out/Release/node - out/Release/ ... header.h - src/node_javascript.cc- wscript

- base64 - base64 diff

12년 6월 23일 토요일

Page 104: [Frends] go together with nodejs

12년 6월 23일 토요일

Page 105: [Frends] go together with nodejs

끝. 감사합니다. KIN플~

12년 6월 23일 토요일