[Node.js] Express.js with Node.js

2023. 2. 14. 13:04Trip to Front-end

 

공부하면서 이해가 안되었던 부분을 정리하고 넘어가도록한다. 

 

1. Node.js의 역할

 

Node.js는 기본적으로 자바스크립트를 브라우저 밖에서도 사용하기 위해 만들어졌으며 프로그래머들이 컴퓨터 하드웨어 장치를 자바스크립트로 직접 상호작용할게 있게 만들었다. 

(Node.js was made to basically liberate Javascript from the browser and allow programmers to use Javascript to interact with the computer directly, for example on a server.)

 

2. Express.js란?

 

Node.js의 프레임워크로 백엔드 구축을 위해서 사용한다. Express.js는 npm 패키지 중 하나로 node가 스크류 드라이버라면 express는 전동 드릴로 비유할 수 있다. 반복작인 작업을 없애준다.

 

3. Listen 메소드에 대하여

 

Express method로 서버에서 보내진 HTTP 요청에 대해 어느 포트로 받을 것인지 말해준다. 

(this tells it to listen on a specific port for any HTTP requests that get sent to out server)

 

4. Get 메소드에 대하여

 

Express method로 브라우저가 우리 서버에 요청이 왔을 때 어떤 내용을 보여줄 것인지 말해준다. 유저가 주소를 입력했을 때 우리 서버에 요청하는 것이고 Get의 내용이 유저에게 보여진다. 첫 번째 인자로 get 요청의 위치를 받는다. 

{Express that allows us to specify what should happen when a browser gets in touch with our server / Now the first parameter it takes is the location of the get request}

(this method, app.get define what should happen when someone makes a get request to the home route)

(there's a callback function that tells the server what to do when that request happens)

 

5. Post에 대하여

 

Get 메소드에서 값을 도출할 때 post를 통해서 알려준다. 

 

6. HTTP 모듈에 대하여 

 

HTTP 모듈은 Node.js의 기본 모듈로 인자로 URL과 콜백 함수를 받아 우리 서버가 다른 서버에서 데이터를 요청할 때 사용한다. 

 

나는 현재 날씨 데이터를 보내는 것을 선호합니다. 그러기 위해 OpenWeatherMap’s server에 Get 요청을 해야하고 JSON 데이터로 받아 사용하기로 합니다.

(I'd really prefer to send the current weather data. In order to do that, I have to somehow make a get request to the OpenWeatherMap’s server, and be able to fetch the data back as a JSON, and parse it)

 

6-1 On 메소드에 대하여

 

우리는 되돌아온 반응(요청으로 받은 데이터)을 이용할 수 있습니다. On 메소드를 통해서 필요한 데이터를 찾아볼 수 있고 활용할 수 있습니다.

(we can also tap into the response that we get back, and call a method called on, and search through it for some data.
Now this will correspond to the actual message body that we got back, that OpenWeatherMap has actually sent us.)

 

* json.parse의 역할 = json 아닌 정보를 json으로 만듬