11/24공부
const see = (req,res)=>{
const {id} = req.pararms;
console.log(id);
const video =videos[id-1] //가짜로만든 데이터베이스 배열은 0에서 부터 시작하기 때문에
res.render("see", {pageTtile:`watching ${video.title}`,);
}
데이터 back-end로 보내기 (form 사용)
form(method="post") // form과 back-end 사이의 정보 전송방식 | form(action = " ") //데이터 어디로 보낼 건지 url
input(name="title") //없음 안됨!! 중요
input(type="submit")
Form // method postrequest 와 getrequest
getrequest 기본값 ex) 네이버, 구글 검색한다. 비디오검색 , 검색어가 주소url 에 포함됨
postrequest 파일을 보내거나 database에 있는 값을 바꾸는 뭔가를 보낼 때 , 수정 , 추가 , 로그인 할 때도 post!
action 안쓰면 똑같은 url로 보냄
//http method는 get, post, delete , patch 등 다양하게 있다.
post request --> back-end로 but, backend는 post require 하는 법 모름
videoRouter.route("/:id(//d+)/edit").get(getEdit).post(postEdit);
getEdit--> painting, postEdit --> 변경사항 저장
같은 경로 일때 shortcut
videoRouter.get("경로" , function);
videoRouter.get("경로" , function);
--> videoRouter.route("경로").get(function).post(function)
post back-end에 데이터 전송은 가능하나 get 가져오는거 할 줄 모름
req.body 사용하고싶지만
express application 은 form을 다루는 방법을 아직 모름
우리는 어필리케이션에게 폼을 다루고싶다고 말해야해
express function method 중 urlencoded 줘야해
app.use(express.urlencoded({extended: true})); // express가 form의 body이해하고 javascirpt로 이용할 수있게 변환 미들웨어
얘가 실행되어야지 req.body 가 생김
const title = req.body.title; //html에 name 속성이있어야 그걸로 호출 가능 !
form을 javascirpt로 바꿔주는 middleware가 router전에 있어서!
request가 videoRouter 에 이르렀을 때 req.body 준비되어있음
+req.body is only with POST :)
+ 몇가지 options parameter limit --> parmas 갯수 제한
limit --> form 사이즈
extended --> body 정보를 form의 데이터를 줄것 javascirpt object 형식ㅇ로
설치 --> server.js
how to send data to backend
post data -> database -> save database
#{ }는 문자열과 함께 쓰는 방식 attribute에는 사용 할 수 없다.
es6 매직
const { id } = req.params;
= const id = req.params. id;
상대경로와 절대경로
/edit 절대경로, root의 /edit
edit 상대경로, 끝부분만 edit으로 바뀜
a(href="/edit") --> rootpage/edit a(href=`/${video.id}/edit`) -->rootpage/123/dit
a(href="edit")--> 현재 주소가 rootpage/video/132이었을 때 rootpage/video/edit a(href="${video.id}/edit")--> rootpage/video/123/edit
res.redirect( ) --> 브라우저가 우리가 준 url로 redirect자동으로 하도록함
--> watch페이지로 이동
const {id } = req.params;
return res.redirect(/videos/${id})
upload video 만들기
처음에 get되어야함 :유저가 form을 볼 수있어야하니까
1.Controller 만들기 : export const getupload 함수만들기
2 컨트롤러 사용 할 Router만들기
3 link 만들기 /upload (라우터에 link, 컨트롤러 적용하기ㅜ이해서)
4.template만들기 pug로 upload template만들기 (form , input )
5.req.body 로 input value 얻어오기
input에 name 없으면 못 얻어옴!!!
6.비디오 생성하기 :post upload 함수에 const newVideo 객체 생성하기
7.videos array에 생성한 비디오 집어넣기 : videos.push(newVideo)