Development/Node.js

시멘틱 URL [Semantic URL]

Tunko 2017. 10. 22. 01:55

주소를 좀더 다듬는다. 

기존 url 은 http://localhost:3000/topic?id=0 이런 방식이였다.
하지만 이를
이런 방식으로 하는 방식이다.
Non-semantic URL Semantic URL
http://example.com/index.php?page=namehttp://example.com/name
http://example.com/index.php?page=consulting/marketinghttp://example.com/consulting/marketing
http://example.com/products?category=12&pid=25http://example.com/products/12/25
http://example.com/cgi-bin/feed.cgi?feed=news&frm=rsshttp://example.com/news.rss
http://example.com/services/index.jsp?category=legal&id=patentshttp://example.com/services/legal/patents
http://example.com/kb/index.php?cat=8&id=41http://example.com/kb/8/41
http://example.com/index.php?mod=profiles&id=193http://example.com/profiles/193
http://en.wikipedia.org/w/index.php?title=Semantic_URLhttp://en.wikipedia.org/wiki/Semantic_URL

이를 비교해보면 된다.

기존 코드
app.get('/topic', function(req, res){
  // res.send(req.query.id);
  var topics = [
    'Javascripte is...',
    'Nodejs is...',
    'Express is...'
  ];
  var output = `
    <a href="/topic?id=0">Javascripte</a><br>
    <a href="/topic?id=1">Nodejs</a><br>
    <a href="/topic?id=2">Express</a><br><br>
    ${topics[req.query.id]}
  `;
  //var output = str + topics[req.query.id];
  res.send(output);
});

시맨틱 적용

app.get(‘/topic/:id', function(req, res){
  // res.send(req.query.id);
  var topics = [
    'Javascripte is...',
    'Nodejs is...',
    'Express is...'
  ];
  var output = `
    <a href=“/topic/0">Javascripte</a><br>
    <a href=“/topic/1">Nodejs</a><br>
    <a href=“/topic/2">Express</a><br><br>
    ${topics[req.params.id]}
  `;
  //var output = str + topics[req.query.id];
  res.send(output);
});

다른 라우터로 복수의 파라미터 시맨틱 URL방법으로 전달

app.get('/topic/:id/:mode', function(req, res){
  res.send(req.params.id+','+req.params.mode)
})


반응형