#3.1 ~ 3.4
.get("URL", "GET handler function")
을 설정하면 GET 요청이 처리되지만 GET 요청에 응답하지 않는다.
서버가 사용자의 GET 요청에 응답하도록 하려면 EventHandler 함수를 화살표 함수로 수정
그런 후 두가지 함수를 사용해서 응답할 수 있다. .end()
, /.send()
(arrow function은 return을 포함한다)
const handlehome = (req, res) => {
return res.send("hi");
};
res.end() - 아무것도 반환하지않고 응답을 종료
res.send() - 유저의 브라우저에 입력을 반환
res.send() 사용시 브라우저
GET handler function의 첫번째 인자는 request object( req
)
두번째 인자는 response object( res
)이어야한다.
( request와 response는 express로 부터 받는 것 )
import express from "express"; //"express"라는 package라는 이름으로 import
const PORT = 4000;
const app = express();
//express application(app이 아닌 다른 이름으로 해도되지만 관습이기때문에 되도록 app으로)
//application설정
const handlehome = (req, res) => {
return res.send("hi");
};
const handelLogin = (req, res) => {
return res.send("Login here");
};
app.get("/", handlehome); //누군가가 root page로 get request를 보낸다면, 함수 작동
app.get("/login", handelLogin);
const handleListening = () =>
console.log(`✅ Server listening on port http://localhost:${PORT} 🚀`);
app.listen(PORT, handleListening);
// 서버가 사람들이 뭐가를 요청할 때까지 기다리게 해야된다.
//외부 접속을 listen
express 문서
https://expressjs.com/ko/4x/api.html#express