돈 많은 백수가 되고 싶어

fs를 이용한 동적 웹페이지 본문

프로그래밍/노드js

fs를 이용한 동적 웹페이지

미하루 2024. 7. 19. 08:58

fs 적용 전

// 모듈 - C#이나 자바로 치면 패키지 같은 것 using, import 등등 (라이브러리에서 불러오는 기능들)
var http = require("http"); //http 요구
var fs = require("fs"); // fs(파일 편집기)
var url = require("url"); //require 요구하다

var app = http.createServer(function (request, response) {
  var _url = request.url;
  var queryData = url.parse(_url, true).query; //parse 분석하다
  var title = queryData.id;
  console.log(queryData.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);
  });
app.listen(3000);

 

 

fs를 적용한 코드

// 모듈 - C#이나 자바로 치면 패키지 같은 것 using, import 등등 (라이브러리에서 불러오는 기능들)
var http = require("http"); //http 요구
var fs = require("fs"); // fs(파일 편집기)
var url = require("url"); //require 요구하다

var app = http.createServer(function (request, response) {
  var _url = request.url;
  var queryData = url.parse(_url, true).query; //parse 분석하다
  var title = queryData.id;
  console.log(queryData.id);
  if (_url == "/") {
    title = "Welcome";
  }
  if (_url == "/favicon.ico") {
    return response.writeHead(404);
  }
  response.writeHead(200);
  fs.readFile(`data/${queryData.id}`, "utf-8", function (err, description) {
    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>${description}</p>
</body>
</html>
  `;
    response.end(template);
  });
});
app.listen(3000);

 

'프로그래밍 > 노드js' 카테고리의 다른 글

자바스크립트 반복문  (0) 2024.07.19
자바스크립트 boolean, 비교연산자, 조건문  (0) 2024.07.19
URL  (0) 2024.07.12
Template Literal  (0) 2024.07.12
자바스크립트 data type  (0) 2024.07.09