🐞 κ°•μ˜ ν•„κΈ°

μ •λ‚˜μ˜Β·2022λ…„ 11μ›” 16일
0
post-custom-banner

URL의 이해

  • protocol : μ‚¬μš©μžκ°€ μ„œλ²„μ— μ ‘μ†ν•˜λŠ” 방식에 λŒ€ν•œ 톡신 κ·œμΉ™
  • host(domain) : 인터넷에 μ ‘μ†λ˜μ–΄ μžˆλŠ” 각각의 컴퓨터 μ£Όμ†Œ
  • port : ν•œ λŒ€μ˜ 컴퓨터 μ•ˆμ— μ—¬λŸ¬ 개의 μ„œλ²„κ°€ μžˆλŠ”λ°, 접속할 λ•Œ port에 μ—°κ²°λ˜μ–΄μžˆλŠ” μ„œλ²„μ™€ 톡신
  • path : μ»΄ν“¨ν„°μ˜ 디렉토리와 νŒŒμΌμ„ 가리킴
  • query string : 데이터 전달

URL을 톡해 μž…λ ₯된 κ°’ μ‚¬μš©ν•˜κΈ°

  • querystring에 따라 λ‹€λ₯Έ 정보 좜λ ₯
  • url 값을 μΆ”μΆœν•΄ μ›ν•˜λŠ” 값을 μ–»μ–΄λ‚Ό 수 있음
    - queryData = {id:HTML}
    - queryData.id = HTML
var _url = require('url');
//urlμ΄λΌλŠ” nodeλŠ” urlμ΄λΌλŠ” λ³€μˆ˜λ₯Ό 톡해 μ‚¬μš©

var _url = request.url;
var queryData = url.parse(_url, true).query; //queryData λ³€μˆ˜μ— λ‹΄κ²¨μžˆλŠ” 값은 객체
var http = require('http');
var fs = require('fs');
var url = require('url');

var app = http.createServer(function(request,response){
    var _url = request.url;
    var queryData = url.parse(_url,true).query;
    if(_url == '/'){
        _url = '/index.html';
    }
    if(_url == '/favicon.ico'){
        response.writeHead(404);
        response.end();
        return;
    }
    response.writeHead(200);
    console.log(__dirname + url);
    response.end(queryData.id);
});
app.listen(3000);
  • module
    : 기본적으둜 μ œκ³΅ν•˜λŠ” κΈ°λŠ₯듀을 λͺ¨μ•„놓은 각각의 κ·Έλ£Ή

동적인 μ›ΉνŽ˜μ΄μ§€ λ§Œλ“€κΈ°

  • 동적 μ›ΉνŽ˜μ΄μ§€
    : μ„œλ²„μ— μ €μž₯λ˜μ–΄ μžˆλŠ” HTML에 데이터 μΆ”κ°€/κ°€κ³΅ν•˜μ—¬ λ³΄μ—¬μ£ΌλŠ” 방법
var http = require('http');
var fs = require('fs');
var url = require('url');

var app = http.createServer(function(request,response){
    var _url = request.url;
    var queryData = url.parse(_url,true).query;
    var title = queryData.id;
    if(_url == '/'){
        title = 'Welcome';
    }
    if(_url == '/favicon.ico'){
        response.writeHead(404);
        response.end();
        return;
    }
    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>
      <ul> 
        <li><a href="/?id=HTML">HTML</a></li>
        <li><a href="/?id=CSS">CSS</a></li>
        <li><a href="/?id=Javascript">JavaScript</a></li>
      </ul>
      <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);

Node.js의 파일 읽기 κΈ°λŠ₯

  • CRUD
    • Create, Read, Update, Delete
  • READ
var fs = require('fs');
fs.readFile('sample.txt','utf8', function(err,data) {
    console.log(data);
})

νŒŒμΌμ„ μ΄μš©ν•΄ λ³Έλ¬Έ κ΅¬ν˜„

post-custom-banner

0개의 λŒ“κΈ€