반응형
javascript기반으로 코드를 작성하고, node에서 지원하는 모듈을 로드(require("모듈이름")) 한다.
1 2 3 4 5 6 7 8 9 10 11 | var http = require("http"); http.createServer(function(request, response){ console.log("Request received."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888); console.log("Server has started"); | cs |
http에 http모듈을 로드하고, http모듈에서 지원하는 createServer() 를 호출한 뒤 리턴 받는 객체에 내장된 listen() 메소드에 포트번호를 할당하는 것으로 서버 구동 완료.
해당 서버를 콘솔에서 node "파일명" 명령어로 실행하면 된다.
그렇게 실행하면
C:\Users\Administrator\data_node>node server.js
Server has started
Request received.
Request received.
이런식으로 서버 접속시 request received 메세지가 두 번 출력되는데, 그 이유는 브라우저가 http://localhost:8888 을 요청할 때, http://localhost:8888/favicon.ico를 로드하려고 하기 때문이다.
반응형
댓글