Dev Web/Node.js2013. 9. 24. 11:32

외부모듈이란 일반 개발자가 만들어 배포한 모듈을 말합니다.

Node.js 는 npm(Node Package Manager)을 기반으로 모듈을 공유합니다.


사용법 : npm install 모듈명

ex )

npm install ejs -> ejs 모듈이 설치됩니다.

npm install jade -> jade 모듈이 설치됩니다.

설치된 모듈 사용은 

var ejs = require('ejs');

var jade = require('jade');


ejs 모듈

ejs 모듈은 템플릿 엔진 모듈 입니다. (View, HTML) 담당.

ex) view.ejs 파일


  1. <% var name = 'RintIanTta'; %>  
  2. <h1></h1>  
  3. <p></p>  
  4. <% for (var i = 0; i < 10; i++) { %>  
  5. <h2>The Squre of <%= i %> is <%= i * i %> </h2>  
  6. <% } %>  

결과

  1. <h1>RintIanTta</h1>  
  2. <p>14196</p>  
  3. <hr />  
  4. <h2>The Squre of of 0 is 0</h2>  
  5. <h2>The Squre of of 1 is </h2>  
  6. <h2>The Squre of of 2 is 4</h2>  
  7. <h2>The Squre of of 3 is 9</h2>  
  8. <h2>The Squre of of 4 is 16</h2>  
  9. <h2>The Squre of of 5 is 25</h2>  
  10. <h2>The Squre of of 6 is 36</h2>  
  11. <h2>The Squre of of 7 is 49</h2>  
  12. <h2>The Squre of of 8 is 64</h2>  
  13. <h2>The Squre of of 9 is 81</h2>  

데이터 전달
js 페이지 (controller) 에서 ejs 페이지에 데이터를 전달하는 방법입니다.


  1. var http = require('http');  
  2. var fs = require('fs');  
  3. var ejs = require('ejs');  
  4.   
  5. http.createServer(function(request, response) {  
  6.   
  7.    //view.ejs 파일을 읽는다.  
  8.   fs.readFile('view.ejs''utf8'function(error, data) {  
  9.      response.writeHead(200, {'content-type' : 'text/html'});   
  10.      response.end(ejs.render(data, {  
  11.          name : 'RintIanTta',  
  12.          description : 'Hello ejs With Node.js.. !'  
  13.      }));  
  14.   });  
  15.   
  16. }).listen(8124, function() {  
  17.       console.log('Server running on 8124');  
  18. });  


'Dev Web > Node.js' 카테고리의 다른 글

[Node.js] express 모듈  (0) 2013.09.24
[Node.js] jade 외부 모듈  (0) 2013.09.24
[Node.js] 기본 내장 모듈  (0) 2013.09.24
[Node.js] Node.js 샘플 애플리케이션  (0) 2013.09.24
[Node.js] Node.js 개요.  (0) 2013.09.24
Posted by 놀란