728x90
🖥️ Query String
쿼리스트링은 사용자가 입력 데이터를 전달하는 방법 중의 하나로, url 주소에 미리 협의된 데이터를 파라미터로 통해 넘기는 것을 말한다.
http://~~/path?querystring
위와 같이 쿼리 스트링은 URL 에서 ? 다음에 오는 내용이다.
이름을 해석하면 간단한데 Query(질문) String(문자열) 즉. ? 뒤에 나오는 문자열이라는 뜻이다.
🖥️ Query String의 구조
http://~~~/path?키1=값1&키2=값2&키3=값3
위와 같이 쿼리스트링은 key=value 형식으로 구성되어있고, 여러 개를 사용하려면 '&'을 사용하면 된다. 위의 예시는 3개의 쿼리스트링을 보내고 있다.
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번으로 지정
728x90
'👨💻 node.js' 카테고리의 다른 글
회원인증 방법론 (session-based, JWT, OAuth) (0) | 2024.02.22 |
---|---|
Not Found 구현 (1) | 2024.02.22 |
URL의 이해 (4) | 2024.02.19 |
Node.js 설치 (0) | 2024.02.19 |
Node.js란 (0) | 2024.02.19 |