dev/데이터베이스

아주 간단한 노드js 웹 어플리케이션

jeongsu 2018. 1. 13. 11:52
const http = require('http');
 
const hostname = '127.0.0.1';
const port = 1337;
 
http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World\n');
}).listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

위의 코드는 node js의 가장 기본적인 웹서버를 구현할 수 있는 코드이다.


http의 모듈을 가져와 http라는 변수에 저장하고


http는 createServer라는 메소드를 사용해 Server객체를 불러오고 있다.


그 객체에서 listen라는 메소드를 다시 실행시켜서 결국 웹요청에 대해 기다리는 프로그램을 만들고 있는 것이다.