Express : 노드에 기반한 백엔드 프레임워크
nodemon : 코드 변경 시 서버를 종료하고 재시작하지 않아도 새로고침으로 변경 사항이 적용되는 패키지
-package-lock.json : 애플리케이션에서 사용하는 여러 패키지 간의 의존성을 기록
선더 클라이언트 : VS Code 안에서 서버 실행 결과를 확인하는 확장
<app.js> : nodemon으로 서버를 실행
※.get() : GET 메소드 지정하는 함수
※res.status() : 응답 상태 코드를 지정하는 함수
※res.send() : 괄호 안에 내용을 화면에 표시하는 함수
const express = require("express"); //express 패키지를 가져옴
const app = express(); //app 변수로 express 기능 사용 가능
app.get("/", (req, res) => { //GET 메서드 지정
res.status(200); //응답 상태 코드
res.send("Hello Node"); //응답을 화면에 표시
});
app.listen(3000, () => { //서버 실행
console.log("3000 포트에서 서버 실행 중");
});
>> nodemon app
<app.js> : /contact URL로 라우팅, POST 메서드 지정, 선더 클라이언트로 라우팅 확인
※메서드 체이닝 : 콜백 함수 안에 두 함수를 연결한 것
※.post() : POST 메서드를 지정하는 함수
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.status(200).send("Hello Node"); //메서드 체이닝
});
app.get("/contact", (req, res) => { //contact 페이지로 라우팅
res.status(200).send("Contact Page");
});
app.post("/contact", (req, res) => { //POST 메서드 지정
res.status(201).send("Create Contact");
});
app.listen(3000, () => {
console.log("3000 포트에서 서버 실행 중");
});
>> nodemon app
<app.js> : 특정 id값에 따라 데이터 처리
※라우트 파라미터 : 요청 URL에 함께 담아서 요청하는 값
-req.params.id : id값을 파라미터로 지정
※.put() : PUT 메서드를 지정하는 함수
※.delete() : DELETE 메서드를 지정하는 함수
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.status(200).send("Hello Node");
});
app.get("/contact", (req, res) => {
res.status(200).send("Contact Page");
});
app.post("/contact", (req, res) => {
res.status(201).send("Create Contact");
});
app.get("/contact/:id", (req, res) => { //특정 id값 데이터 가져오기
res.status(200).send(`View Contact for ID : ${req.params.id}`);
});
app.put("/contact/:id", (req, res) => { //PUT 메서드 지정, 특정 id값 데이터 수정
res.status(200).send(`Update Contact for ID : ${req.params.id}`);
});
app.delete("/contact/:id", (req, res) => { //DELETE 메서드 지정, 특정 id값 데이터 삭제
res.status(200).send(`Delete Contact for ID : ${req.params.id}`);
});
app.listen(3000, () => {
console.log("3000 포트에서 서버 실행 중");
});
>> nodemon app
스크립트를 사용해서 파일 실행하기 (npm start)
<package.json>
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js" //스크립트 명령 지정
}
>> npm start
'Do it Node.js > EJS BackEnd' 카테고리의 다른 글
12. 몽고DB, 스키마 (0) | 2024.03.08 |
---|---|
11. 미들웨어 (0) | 2024.03.06 |
9. Node 비동기 처리 (0) | 2024.03.01 |
8. 라우팅 (0) | 2024.03.01 |
7. HTTP 모듈 (0) | 2024.02.29 |