외부모듈이란 일반 개발자가 만들어 배포한 모듈을 말합니다.
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 파일
- <% var name = 'RintIanTta'; %>
- <h1></h1>
- <p></p>
- <% for (var i = 0; i < 10; i++) { %>
- <h2>The Squre of <%= i %> is <%= i * i %> </h2>
- <% } %>
결과
- <h1>RintIanTta</h1>
- <p>14196</p>
- <hr />
- <h2>The Squre of of 0 is 0</h2>
- <h2>The Squre of of 1 is </h2>
- <h2>The Squre of of 2 is 4</h2>
- <h2>The Squre of of 3 is 9</h2>
- <h2>The Squre of of 4 is 16</h2>
- <h2>The Squre of of 5 is 25</h2>
- <h2>The Squre of of 6 is 36</h2>
- <h2>The Squre of of 7 is 49</h2>
- <h2>The Squre of of 8 is 64</h2>
- <h2>The Squre of of 9 is 81</h2>
데이터 전달
js 페이지 (controller) 에서 ejs 페이지에 데이터를 전달하는 방법입니다.
- var http = require('http');
- var fs = require('fs');
- var ejs = require('ejs');
- http.createServer(function(request, response) {
- //view.ejs 파일을 읽는다.
- fs.readFile('view.ejs', 'utf8', function(error, data) {
- response.writeHead(200, {'content-type' : 'text/html'});
- response.end(ejs.render(data, {
- name : 'RintIanTta',
- description : 'Hello ejs With Node.js.. !'
- }));
- });
- }).listen(8124, function() {
- console.log('Server running on 8124');
- });
'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 |