Node.js #7 (query string)

박준석·2023년 3월 14일
0

Node.js

목록 보기
7/12
post-thumbnail

query string

var url = require('url');

require는 요구하다는 것이다. 즉 url 정보를 요구하는 것이다.

var _url = request.url;
  var queryData = url.parse(_url, true).query; //query string을 불러 온다.
  var title = queryData.id; //query id를 가져오는 것이다.
  if (_url == '/') {
    title = 'Welcome'
  }
  if (_url == '/favicon.ico') {
    return response.writeHead(404);
  }
  response.writeHead(200);
  var template = `
  <!doctype html>
  <html>
  <head>
    <title>WEB1 - ${title}</title>
    <meta charset="utf-8">
  </head>
  <body>
    <h1><a href="/">WEB</a></h1>
    <ol>
      <li><a href="/?id=HTML">HTML</a></li>
      <li><a href="/?id=CSS">CSS</a></li>
      <li><a href="/?id=JavaScript">JavaScript</a></li>
    </ol>
    <h2>${title}</h2>
    <p><a href="https://www.w3.org/TR/html5/" target="_blank" title="html5 speicification">Hypertext Markup Language (HTML)</a> is the standard markup language for <strong>creating <u>web</u> pages</strong> and web applications.Web browsers receive HTML documents from a web server or from local storage and render them into multimedia web pages. HTML describes the structure of a web page semantically and originally included cues for the appearance of the document.
    <img src="coding.jpg" width="100%">
    </p><p style="margin-top:45px;">HTML elements are the building blocks of HTML pages. With HTML constructs, images and other objects, such as interactive forms, may be embedded into the rendered page. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. HTML elements are delineated by tags, written using angle brackets.
    </p>
  </body>
  </html>  
  `;
  response.end(template);//이 코드가 사용자가 실행하고 싶은 파일을 넣는다.
  //javascript를 통해서 우리가 읽어드려야 하는 파일을 만든 것이다.
  // 사용자가 생성한 파일을 전송한다.
});
app.listen(3000); //포트 번호를 3000번으로 지정

다음과 같이 리스트를 눌렀을 때 제목이 바뀌는 것을 볼 수 있다.

profile
안녕하세요 프론트엔드 개발자입니다. 글을 이전 중입니다.

0개의 댓글