728x90
반응형
웹 프레임워크(express 등)를 사용하지 않고 http 모듈을 사용하여 서버를 구축하는 방법
간단한 로직인 경우에는 express로 구축하는 것 보다 http 모듈을 활용하는 것이 성능적으로 더 뛰어남
예시 소스
'use strict'
const http = require('http');
// 서버 생성
const server = http.createServer((req, res) => {
// 요청에 대한 결과를 보낼 때 200일 경우 정상 작동
res.statusCode = 200
// 해당하는 데이터로 어떠한 것을 보냈는지 명시적으로 선언
res.setHeader('Content-type', 'text/html'); // html 문서를 리턴한다.
// 종료
res.end('<dvi>Hello World</div>');
})
// 명시적으로 포트를 외부에서 받음
//const port = process.env.PORT
// 테스트 화면을 띄우기 위해 임의의 포트 번호 작성
const port = 3000;
server.listen(port, () => {
console.log(`Server running at port ${port}`);
})
728x90
반응형
'Javascript > Node' 카테고리의 다른 글
[Node] Class 활용하여 express 선언하기 (0) | 2020.10.13 |
---|---|
[Node] Prototype vs Class 차이점 알아보기 (0) | 2020.10.13 |
[Node] Promise.race 사용해보기 (0) | 2020.09.29 |
[Node] promise.all 사용해보기 (0) | 2020.09.28 |
[Node] File System 읽기와 쓰기(callback 방식과 async await 방식) (0) | 2020.09.28 |