본문 바로가기
Develop/Node.js

Node.js 설치 및 시작하기

by 라이프레이서 2019. 12. 19.
반응형

1. 설치하기

https://nodejs.org/ko/

 

Node.js

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

nodejs.org

Node.js 공식 홈페이지 가서 다운로드 받아주면 된다. 그냥

설치하는 과정은 별 특이사항이 없다.

이후 잘 돌아가는지 테스트를 위해서 js파일을 만들어줘야 한다. js 파일은 atom editor / vscode 등 많은 편집기가 있는데 필자는 vscode를 사용하기로 한다.

https://code.visualstudio.com/

 

Visual Studio Code - Code Editing. Redefined

Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.

code.visualstudio.com

이후 webserver.js 파일을 만들어준다. 해당 코드는 아래와 같다.

const http = require('http');

http.createServer((request, response) => {
    response.writeHead(200, {'Content-Type' : 'text/plain'});
    response.end('Hello world\n');
}).listen(5000);

console.log('Server running at http://127.0.0.1:5000/');

이후 콘솔창에서 해당 파일을 실행시켜주면 된다.

윈도우키 + R 을 눌러 실행창을 띄우고, cmd를 입력한 후 확인을 눌러준다.

webserver.js가 들어있는 경로로 이동 후 실행시켜주기만 하면 된다.


<참고> - cmd 명령어

cd : 해당 경로로 이동하도록 한다.

ex) 현재 경로가 C:\Users 이고, 이동하고 싶은 경로가 C:\Users\node 라면

cd node 를 입력 후 엔터를 치게 되면 해당 디렉토리로 이동이 된다.

ex2) 현재 경로가 C:\Users 이고, 이동하고 싶은 경로가 C:\Program Files 라면

cd C:\Program Files 라 입력 후 엔터를 치면 된다.

 

cd.. : 상위 디렉토리로 이동한다.

ex) 현재 경로가 C:\Users\node 인 경우 cd..를 치게 되면

C:\Users 로 변경이 된다.


webserver.js가 있는 디렉토리로 이동한 후, node webserver.js 를 입력하면 해당 js파일이 실행 된다.

이후 웹 브라우저를 실행하여

http://127.0.0.1:5000 을 입력하면

짠! 서버가 켜졌고, Hello world를 보내는 걸 확인할 수 있다.

서버를 종료하고 싶으면 cmd창에서 Ctrl+C를 누르거나, cmd 창을 종료하면 된다.

반응형