NOde.js HTTP 모듈
NOde.js HTTP 모듈
내장 HTTP 모듈
Node.js에는 Node.js가 HTTP(Hyper Text Transfer Protocol)를 통해 데이터를 전송할 수 있게 해줍니다.
HTTP모듈을 포함 시키려면 다음 require()을 사용하면 됩니다.
var http = require("http");
웹 서버로서의 Node.js
HTTP모듈은 서버 포트를 수신하고 클라이언트에 응답을 제공하는 HTTP서버를 만들 수 있습니다.
이 createSerser()방법을 사용하여 HTTP서버를 만듭니다.
예)
var http= require("http");
http.createServer(function(req, res){
res.write("Hello World!");
res.end();
}).listen(8080);
http.createServer()메서드에 전달 된 함수는 누군가가 포트8080에서 컴퓨터에 액세스하려고 시도할 때 실행됩니다.
위의 코드를 "demo_http.js"라는 파일로 저장하세요.
실행방법
C:\Users\Your Name\node demo_http.js
HTTP헤더 추가
HTTP서버의 응답을 HTML로 표시하려면 올바른 내용 유형의 HTTP헤더를 포함해야 합니다.
예)
var http = require("http");
http.createServer(function(req, res){
res.writeHead(200, {'Content-Type' : 'text/html'});
res.write("hello world!");
res.end()
}).listen(8080);
res.writeHead()메소드의 첫번째 인수는 상태코드이고 , 200은 모두 ok이고
두 번째 인수는 응답 헤더가 들어있는 객체입니다.
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4766 | jQuery | 8년 전 | 1679 | ||
| 4765 | jQuery | 8년 전 | 1350 | ||
| 4764 | jQuery | 8년 전 | 2168 | ||
| 4763 | jQuery | 8년 전 | 1856 | ||
| 4762 | jQuery | 8년 전 | 1634 | ||
| 4761 | jQuery | 8년 전 | 1935 | ||
| 4760 | jQuery | 8년 전 | 1299 | ||
| 4759 | jQuery | 8년 전 | 1599 | ||
| 4758 | jQuery | 8년 전 | 1313 | ||
| 4757 | jQuery | 8년 전 | 1280 | ||
| 4756 | jQuery | 8년 전 | 1497 | ||
| 4755 | jQuery | 8년 전 | 1650 | ||
| 4754 | jQuery | 8년 전 | 1616 | ||
| 4753 | jQuery | 8년 전 | 1772 | ||
| 4752 | jQuery | 8년 전 | 1522 | ||
| 4751 | jQuery | 8년 전 | 1841 | ||
| 4750 | jQuery | 8년 전 | 1982 | ||
| 4749 | jQuery | 8년 전 | 1442 | ||
| 4748 | PHP | 8년 전 | 2811 | ||
| 4747 | jQuery | 8년 전 | 1393 | ||
| 4746 | jQuery | 8년 전 | 1650 | ||
| 4745 | jQuery | 8년 전 | 1658 | ||
| 4744 | jQuery | 8년 전 | 1537 | ||
| 4743 | jQuery | 8년 전 | 1614 | ||
| 4742 | jQuery | 8년 전 | 1708 | ||
| 4741 | jQuery | 8년 전 | 1600 | ||
| 4740 | jQuery | 8년 전 | 1610 | ||
| 4739 | jQuery | 8년 전 | 1798 | ||
| 4738 | jQuery | 8년 전 | 1315 | ||
| 4737 | jQuery | 8년 전 | 2100 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기