서버는 항상 켜져있는 컴퓨터다. 브라우저는 GET, listen과 같은 Request를 대신해서 서버에 요청한다. 서버는 작성된 middleware에 따라서 Request에 대해 응답한다.
// node_modules 폴더에서 express 모듈을 찾아온다
import express from "express";
// 사용할 포트를 지정한다
const PORT = 5001;
// Server Application을 생성한다
const app = express();
// express server application에 GET request를 보낸다
// Route(URL)은 / 이다
app.get('/', (req,res) => {
res.send(`Welcome to port ${PORT}`); // respons 객체로 하여금 문자열을 보낸다
});
// Route는 /login 이다.
// /login 로, 다음과 같은 GET Request를 보낸다
app.get('/login', (req,res)=> {
res.send('Please enter username & ID');
});
app.listen(PORT, ()=> {
console.log(`Server is listening on port ${PORT}`);
});
라우팅은 URI(또는 경로) 및 특정한 HTTP 요청 메소드(GET, POST 등)인 특정 엔드포인트에 대한 클라이언트 요청에 애플리케이션이 응답하는 방법을 결정하는 것을 말합니다. 각 라우트는 하나 이상의 핸들러 함수를 가질 수 있으며, 이러한 함수는 라우트가 일치할 때 실행됩니다. 라우트 정의에는 다음과 같은 구조가 필요합니다.
app.METHOD(PATH, HANDLER)
app.get('/login', (req,res) => {
res.send(`Welcome to port ${PORT}`);
});
loging URL(/login) 또는 라우트(Route)에 대한 GET 요청에 'Welcome to port'로 응답한다.