코린이의 기록

[React] Node.js 로 간단한 웹 애플리케이션 만들기기 본문

Framework/React

[React] Node.js 로 간단한 웹 애플리케이션 만들기기

코린이예요 2018. 6. 10. 17:22
반응형

간단한 웹 어플리케이션 만들기

hello-server.js

1
2
3
4
5
6
7
8
9
10
11
const http = require('http')             // http 모듈을 읽는다.
 
const svr = http.createServer(handler)    // Web server를 생성한다.
svr.listen(8081)                        // 8081 포트를 사용함
 
// server에 접근하면 실행되는 함수
function handler (req, res){
  console.log('url:', req.url)
  console.log('method:', req.method)
  res.writeHead(200, {'Content-Ttpe''text/html'})    // http 헤더를 출력
  res.end('<h1>Hello, World!</h1>\n')                // 응답 본문 
}
cs

- createServer 및 listen을 통해서 웹 서버를 실행한다. 

- handler 함수는 콜백 함수로 서버에 접근하면 실행된다. req 매개변수에는 요청정보, res 매개변수에는 응답정보이다. 

 

위 hello-server.js 를 실행 한 후 "http://localhost:8081"에 접속해보자


$ node hello-server.js


URL 접속 후에 아래와 같은 문구가 출력되는 것을 확인할 수 있다. 

yunsoyeon-ui-MacBook-Air:ch1 yunsoyeon$ node hello-server.js 

url: /

method: GET


 req.url 로 출력된 url 은 '/'로 출력하였고

req.method로는 GET을 출력하고 있다. 


반응형
Comments