var mkdirp = require('mkdirp');

mkdirp('/tmp/foo/bar/baz', function (err) {
    if (err) console.error(err)
    else console.log('pow!')
});

 

node.js file system 을 확장 해서 사용하는 라이브러리 유용하다~~.

 

https://github.com/substack/node-mkdirp

 

 


Nodejs에서 Query를 이용 할 경우 Java에서 JDBC를 이용한 것이 JavaScript 라고 생각 하면 된다.


JDBC



// STEP1: Query 작성
PreparedStatement preparedStatement = connection.prepareStatement("select * from tb_test where test_no = ? and test_title = ? and test_content = ?");
   
// STEP2: 조건 처리
preparedStatement.setInt(1, 2);

preparedStatement.setString(2, "title");

preparedStatement.setString(3, "content");



Nodejs



client.query('select * from tb_test where test_no = $1 and test_title = $2 and test_content = $3',

[2, 'title', 'content'],

function(error, result) {

// result...

})



위와 같은 모습은 기본 형태라고 볼 수 있다.


Java는 set 하는 순간에 type을 다 정해주기 때문에 고민 할 필요가 없지만 javascript는 동적으로 타입이 변하기 때문에 문제가 발생 한다.

IN 절을 썼을 때 ',' 과 숫자가 결합이 되는 형태가 되면 문자열로 인식 하게 되어 Error가 발생 할 수 있다.


해결 방법



var nos = '1, 2, 3, 4, 5';


client.query('select * from tb_test where test_no in (' + nos + ') and test_title = $1 and test_content = $2',

['title', 'content'],

function(error, result) {

// result...

})



위와 같이 문자열로 직접 결합 해주어야 해결 할 수 있다.


'Nodejs' 카테고리의 다른 글

node-gyp rebuild 시 command not found 해결 방법  (0) 2013.04.26
file system library mkdirp  (0) 2013.03.29
Debian Linux에서 Eclipse, Nodejs 연동하기  (0) 2013.02.16
Nodejs is that ?  (0) 2013.02.10
Nodejs 설치 하기  (0) 2013.02.10


Step 1.  Eclipse 설치하기

aptitude package가 없다면 apt-get 으로 설치 한다.



whitelife@whitelife-server ~/work $ sudo aptitude install eclipse

[sudo] password for whitelife: 


whitelife@whitelife-server ~/work $ eclipse



설치가 완료 되면 위와 같은 명령어를 입력 하거나 Menu에 개발을 확인 하면 Eclipse가 있다. 실행 하자.



위 화면을 봤다면 성공 이다.


Step 2.  Nodeclipse Plugin 설치 하기

Help -> Install New Software... 들어 가기

Add 버튼을 누른 후 추가하자.



Nodeclipse 목록이 뜰 것이다. Next > Finish 누르면 설치가 된다.



설치 중 아래와 같은 화면이 나오면 OK 하자.



설치가 완료 되면 재 시작 하자.


Step 3.  Nodeclipse 설정 하기

node, nodemon 의 경로를 찾는다.



whitelife@whitelife-server ~/work $ which node
/usr/bin/node
whitelife@whitelife-server ~/work $ which nodemon
/usr/bin/nodemon


Window -> Preferences .. 들어가기

위에서 찾은 경로를 넣어주자.

nodemon 은 source가 바뀌어도 자동으로 갱신 해준다.

설치 방법은 http://blog.whitelife.co.kr/entry/nodemon-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0 참고하기



Step 4.  Ansi Console 설치하기

Help -> Install New Software... 들어 가기

Add 버튼을 누른 후 추가하자. 패스 할 경우 한글이 깨 진다.



나머지 부분은 Step 2 와 동일 하다.

설치가 완료 되면 재 시작 하자.


Step 5.  Hello World  찍기

소스를 작성 한 후 Hello.js 파일 마우스 오른쪽 Run AS -> Node Applicatoin 누르기

아래와 같이 메시지를 확인 할 수 있다.



'Nodejs' 카테고리의 다른 글

file system library mkdirp  (0) 2013.03.29
Nodejs Query IN 절 이용 하기  (0) 2013.02.18
Nodejs is that ?  (0) 2013.02.10
Nodejs 설치 하기  (0) 2013.02.10
Html을 Jade로 변환하기  (0) 2013.02.06


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.

Nodejs는 real-time application에 적합하고, event-driven, non-blocking I/O 를 사용한다.


Resolve 1.  real-time application



고객들의 요구 사항이 높아지면서 기술적으로 실시간(real-time) 을 구현해야 한다. 대표적으로 네이트온 웹 메신저, 페이스북 채팅 등이 있다. Nodejs는 real-time application이 필요로 할 때 대응이 빠르다. Socket.io module를 이용하면 간단하게 구현이 가능 하다. non-blocking I/O 이기 때문이다. Java 기반으로 구현을 해야할경우 Comet, Dwr 등을 이용해야 하는데 많은 시간이 요구 되고 난이도가 있는 편이다. 


Resolve 2.  event-driven



실행 절차

a:  Client는 Server로 요청 한다. - event 발생

b:  Server는 Client로 응답 한다. - callback


구조


server.on(event, callback);



사용 예.


server.on('request', function(request, response){

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

        res.write('Hello World');

        res.end();

});



결론

Nodejs는 요청이 들어올때마다 Thread를 생성 하는 서버와는 다르게 단일 Thread이고 요청이 들어올때 마다 Event를 발생 시키고 요구 조건이 다 처리가 되면 Callback으로 응답을 한다. 이와 같은 방식은 메모리를 효율적으로 사용 하고, 많은 동시 접속자를 수용 할 수 있다.


Resolve 3.  non-blocking I/O



Nodejs는 요청이 들어올때마다 Event를 발생시킨다. blocking 방식과는 다르게 계속 추가적인 Event를 수용할 수 있다.


'Nodejs' 카테고리의 다른 글

Nodejs Query IN 절 이용 하기  (0) 2013.02.18
Debian Linux에서 Eclipse, Nodejs 연동하기  (0) 2013.02.16
Nodejs 설치 하기  (0) 2013.02.10
Html을 Jade로 변환하기  (0) 2013.02.06
Nodejs의 의미와 자바의 대안  (0) 2013.02.05


Step 1.  Nodejs 설치 하기

Ubuntu에서 설치 하는 방법이다. 아래의 명령어를 실행 하자.



sudo apt-get install python-software-properties python g++ make

sudo add-apt-repository ppa:chris-lea/node.js

sudo apt-get update

sudo apt-get install nodejs npm



Version 차이가 있을 수 있으므로 저장소를 변경 한후 설치 한다.


버전 확인 하기


whitelife@whitelife ~/Desktop $ apt version nodejs

0.8.19-1chl1~precise1



원문은 Wiki를 참고 하면 된다. https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager


Step 2.  plugin 설치 하기

connect module의 확장 형태로 만들어진 express framework, js 파일이 수정 되면 자동으로 컴파일 해주는 nodemon 자주 사용 되기 때문에 -g 옵션을 주어 Global로 설치 한다. 추가로 사용 하는 plugin이 있다면 설치 하자.



sudo npm install -g express

sudo npm install -g nodemon



'Nodejs' 카테고리의 다른 글

Debian Linux에서 Eclipse, Nodejs 연동하기  (0) 2013.02.16
Nodejs is that ?  (0) 2013.02.10
Html을 Jade로 변환하기  (0) 2013.02.06
Nodejs의 의미와 자바의 대안  (0) 2013.02.05
nodemon 사용하기  (0) 2013.02.04


아래의 페이지에서 Html -> Jade converter 한다. 간편하게 이용이 가능 하다.




'Nodejs' 카테고리의 다른 글

Nodejs is that ?  (0) 2013.02.10
Nodejs 설치 하기  (0) 2013.02.10
Nodejs의 의미와 자바의 대안  (0) 2013.02.05
nodemon 사용하기  (0) 2013.02.04
[스크랩] 완벽 튜토리얼 정리  (0) 2013.02.04


보다가 내용이 유익해서 올림.


※ 참고 사이트: http://www.slideshare.net/JieunLee5/3-nodejs


'Nodejs' 카테고리의 다른 글

Nodejs 설치 하기  (0) 2013.02.10
Html을 Jade로 변환하기  (0) 2013.02.06
nodemon 사용하기  (0) 2013.02.04
[스크랩] 완벽 튜토리얼 정리  (0) 2013.02.04
Nodejs로 WebServer 띄우기  (0) 2012.11.18


nodejs, npm이 설치 되어 있다고 가정 한다.

매번 컴파일 해야 하는 부담감을 줄여주고 새로 작성된 코드를 자동으로 컴파일 해준다.


Step 1.  nodemon 설치하기

npm grobal 옵션을 사용하여 설치 한다.



whitelife@whitelife-note:~/workspace/HelloWorld$ sudo npm install nodemon -g
[sudo] password for whitelife:
npm http GET https://registry.npmjs.org/nodemon
npm http 304 https://registry.npmjs.org/nodemon
/usr/bin/nodemon -> /usr/lib/node_modules/nodemon/nodemon.js
nodemon@0.7.2 /usr/lib/node_modules/nodemon


Step 2.  테스트 하기

node xxx.js -> nodemon xxx.js 로 실행 한다. 소스가 수정 될 경우 자동으로 적용 된다.



whitelife@whitelife-note:~/workspace/HelloWorld$ nodemon index.js
4 Feb 15:37:25 - [nodemon] v0.7.2
4 Feb 15:37:25 - [nodemon] watching: /home/whitelife/workspace/HelloWorld
4 Feb 15:37:25 - [nodemon] starting `node index.js`
Server has started.


// ...


4 Feb 15:43:03 - [nodemon] starting `node index.js`
Server has started.


'Nodejs' 카테고리의 다른 글

Html을 Jade로 변환하기  (0) 2013.02.06
Nodejs의 의미와 자바의 대안  (0) 2013.02.05
[스크랩] 완벽 튜토리얼 정리  (0) 2013.02.04
Nodejs로 WebServer 띄우기  (0) 2012.11.18
Nodejs로 Hello World! 띄우기  (0) 2012.11.18

+ Recent posts