File System 모듈을 사용한 HTML 페이지 제공 방법입니다.
서버에 접속하면 서버에서는 HTML 파일을 읽은 다음 그 내용을 HTML 형식으로 뿌려주는 예제입니다.
File System 모듈의 readFile메소드를 사용하여 html 파일을 읽습니다.
writeHead(statuscode, object);
여기서 statuscode 는 다음 링크에 설명되어있습니다.
http://ko.wikipedia.org/wiki/HTTP_%EC%83%81%ED%83%9C_%EC%BD%94%EB%93%9C
end 메소드는 실제로 본문을 작성하는 부분입니다.
server.js
//모듈을 추출합니다.
var fs = require('fs');//file system 모듈 불러오기
var http = require('http');//http 모듈 불러오기
//웹서버를 생성하고 실행합니다.
http.createServer(function(request, response){
//HTML 파일을 읽습니다.
fs.readFile('HTMLPage.html', function(error, data){
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(data);
});
}).listen(52273, function(){
console.log('Server Running at http://127.0.0.1:52273');
});
HTMLPage.html
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>hello node.js</h1>
<h2>author. rintiantta</h2>
</body>
</html>
결과 화면
hello node.js author. rintiantta |
'프로그래밍 > Node.js' 카테고리의 다른 글
Node.js - POST 요청 Node.js에서 처리 방법 (0) | 2015.01.02 |
---|---|
Node.js - URL별로 다른 HTML 페이지 제공하기 (0) | 2015.01.02 |
Node.js - Location 속성을 이용한 페이지 강제 이동 시키기 (0) | 2015.01.02 |
Node.js - 포트2개를 사용한 이미지와 음악파일 서버 만들기 (0) | 2015.01.02 |
Node.js - mysql 데이터베이스 연동 방법 (0) | 2015.01.01 |
Node.js - EventEmitter 객체를 활용한 이벤트 생성 및 처리 (0) | 2014.12.31 |
Node.js - File System 모듈 사용하기 (0) | 2014.12.31 |
Node.js- ajax로 타 도메인과 연동 문제 / CORS(Cross-Origin Resource Sharing) (0) | 2014.12.27 |