[Node.js]서버 생성하기

간단하네..

1
2
3
4
5
6
7
8
9
10
11
12
13
const  http = require('http');
// 읽는 키워드
 
function rqLis(req,res){ //request,response
 
}
 
 
http.createServer(rqLis);  // 서버 생성 메서드
 
 
 
//require -- > 파일을 불러오는 키워드
cs

첫번째 방법

 

1
2
3
4
5
6
const  http = require('http');
 
 
http.createServer(function(req,res){           
// 익명의 함수를 호출하는것
})
cs

2번째 방법

1
2
3
4
5
6
const  http = require('http');
 
 
http.createServer((req,res) =>{
//콜백함수 = > 이것만으로 사용   
})
cs

3번째 방법

 

이렇게 쫙 출력되면 서버가 생성됫어요

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const  http = require('http');
 
 
const server = http.createServer((req,res) =>{
    console.log(req);
})
 
 
 
server.listen(3000);       포트번호       // node.js 가 들어오는 요청에대해서 계속 데이터 받기
 
이렇게 하면 
 
 
웹페이지에서 localhost:3000 을 치고 들어가보면 터미널에 쫙 데이터ㄱ
cs