IT/Node.js
[노드JS]node.js 예제 실행하기(vscode)
호유(ho_you)
2020. 2. 2. 21:53
반응형
예제로 node.js 의 웹서버 코드를 복사하여
vscode 에서 실행해보도록 하죠.
1. TERMINAL 탭에서 아래 명령어 실행
- 명령어 : node 파일명.js
- ex) node server.js
( 파일이 있는 디렉토리로 이동하여 진행해주세요)
2. 브라우저에서 해당IP로 접속하여 페이지 확인
#소스코드
var http = require('http'); //require은 자바의 import와 같은 역할을 함+객체생성 또는 함수 객체
http.createServer(function(req,res){
res.writeHead( //HTTP Response 헤더에 쓰기
200,//상태코드, 500-서버에러, 404-파일없음, 302-이동
{'Content-Type':'text/plain'}//일반 문자열을 나타냄
)
res.write('hellworld\n');
res.end('Success!\n'); //출력
}).listen(1337,'127.0.0.1'); //대기 포트번호, IP주소
console.log('Server running at http://127.0.0.1:1337/');
반응형