내맘대로 살기🎉
[Node.js] express 쿼리스트링(query string), 시멘틱 URL 본문
ex ) localhost/router?id=1
localhost/router?id=2
localhost/router?id=3
? 뒤에 나타는 것{id=(1~3)}을 query string이라고 한다.
응답, 요청
(req), (res)
res.send(req.query.id); 이렇게 하고 router?id=1이라고 url을 작성하면 html에 1이 출력.
* expressjs.com에 접속하여 API reference를 보는 것도 좋다.
localhost/topic?id=1&name=harris
id와 name 둘 다 가져온다. 중간에 연결자는 &이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | app.get( '/topic/:id' , function (req, res){ var topics = [ 'Javascript is...' , 'Nodejs is...' , 'Express is...' ]; var output = ` <a href= "/topic?id=0" >JavaScript</a><br> <a href= "/topic?id=1" >Nodejs</a><br> <a href= "/topic?id=2" >Express</a><br><br> <!--${topics[req.query.id]} --><!-- Query string 방식--> ${topics[req.params.id]} <!-- Semantic URL 방식 --> ` res.send(output); // req에 query가 있고 사용자의 id(query string)를 속성(property)으로 갖는다. }); |
- query string을 이용하여 화면 상에 link를 만들어주고 선택된 link도 화면 상에 보여준다.
- query string방식과 Semantic URL방식을 비교.
- query string방식은 위의 코드에서 app.get('/topic', func~~~~)라고 써줘야한다.
시멘틱(Semantic) URL
쿼리 스트링이 없이 깔끔하게 보여주고 싶을 때 사용.
*출처 : 위키피디아
- 비교한 표를 보면 한 번에 확인이 가능하다.
'개발자의 길 > Node.js' 카테고리의 다른 글
[Node.js] supervisor를 이용해서 node.js를 쉽게 업로드하자. (0) | 2017.04.27 |
---|---|
[Node.js] express. POST방식을 이용한 정보 전달 (1) | 2017.04.27 |
[Node.js] express 정적&동적, 템플릿 엔진 Jade (0) | 2017.04.26 |
[Node.js] express 정적인 파일 저장하기 (0) | 2017.04.25 |
[Node.js] express의 hello word 예제 (..syntax highlighter 사용법) (0) | 2017.04.25 |