목록

node.js

Home

Hello node.js

# node.js 개발 환경 만들기

node.js 공식 홈페이지를 방문하여 자신에 운영체제에 맞는 버전을 다운로드 및 설치하기

# node.js 실행 해보기

콘솔 환경에서 node 실행해보기

">" 프롬프트가 뜨면 console.log("Hello node.js"); 를 입력해서 위 화면과 같이 뜨면 설치 성공
※ 위와 같이 실행하는 방식을 REPL(Read Eval Print Loop)라고 한다.

# 스크립트 만들기

위의 console.log("Hello node.js");를 텍스트 파일로 만들고 확장자를 js로 만든 다음 node [파일명]으로 실행이 가능하다.

다음을 추가로 해보자

var http = require('http'); // 웹 서버 모듈을 로딩 http.createServer( function (request, response){ response.writeHead(200, { 'Content-Type' : 'text/html' } ); response.end('<h1>hello node.js http module server</h1>'); } ).listen(18888, function () { console.log("Server running at http://127.0.0.1:18888/"); } ); 위의 소스를 httptest.node.js파일로 저장하고 node httptest.node를 실행해보자. 정상적으로 실행이 된다면 브라우저를 열어 localhost:18888 주소로 이동하면 "hello node.js http module server"문구를 볼수 있다. 이 소스는 http모듈을 읽어와 웹서버를 만들어주는 간단한 샘플이다.