| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
                            Tags
                            
                        
                          
                          - GPT-4
 - LLM
 - 지도학습
 - regression
 - OpenAI
 - ChatGPT
 - AI
 - LG Aimers 4th
 - 오블완
 - 분류
 - 머신러닝
 - 해커톤
 - 회귀
 - LG Aimers
 - deep learning
 - supervised learning
 - PCA
 - Classification
 - gpt
 - Machine Learning
 - LG
 - 티스토리챌린지
 - 딥러닝
 
                            Archives
                            
                        
                          
                          - Today
 
- Total
 
SYDev
[Node.js] 9. 함수 본문
해당 게시물은 유튜브 생활코딩 "Node.js" 강의 영상을 참고했습니다.
(https://www.youtube.com/watch?v=3RS_A87IAPA&list=PLuHgQVnccGMA9QQX5wqj6ThK7t2tsGxjm&index=1)
함수
function sum(first, second) {   //parameter: 함수를 정의할 때 사용되는 변수
    return first+second;
}
console.log(sum(2, 4))   //argument: 실제로 함수가 호출될 때, 넘기는 변수값
6
>> c에서의 함수와 거의 유사함
함수를 이용한 정리정돈
var http = require('http');
var fs = require('fs');
var url = require('url');
function templateHTML(title, list, body) {
  return `
  <!doctype html>
  <html>
  <head>
    <title>WEB1 - ${title}</title>
    <meta charset="utf-8">
  </head>
  <body>
    <h1><a href="/">WEB</a></h1>  
    ${list}
    ${body}
  </body>
  </html>
  `;
}
function templateList(filelist) {
  var list = '<ol>';
  var i = 0;
  while(i < filelist.length) {
    list += `<li><a href="/?id=${filelist[i]}">${filelist[i]}</a></li>`;
    i++;
  }
  list += '</ol>';
  return list;
}
var app = http.createServer(function(request,response){
    var _url = request.url;
    var queryData = url.parse(_url, true).query;
    var pathname = url.parse(_url, true).pathname
  
    if(pathname === '/') {
        if(queryData.id === undefined) {
          fs.readdir('./data', function(error, filelist) {
            var title = 'Welcome';
            var description = 'Hello, Node.js';
            var list = templateList(filelist);
            var template = templateHTML(title, list, `<h2>${title}</h2>${description}`);
            response.writeHead(200);
            response.end(template);
          }) 
        } else {
          fs.readdir('./data', function(error, filelist) {
            fs.readFile(`data/${queryData.id}`, 'utf8', function(err, description) {
              var title = queryData.id;
              var list = templateList(filelist);
              var template = templateHTML(title, list, `<h2>${title}</h2>${description}`);
              response.writeHead(200);
              response.end(template);
            });
          });
        }
    } else {
      response.writeHead(404);
      response.end('Not found')
    }
});
app.listen(3000);
>> 함수 templateHTML과 templateList를 이용하여 HTML과 list의 중복되는 표현은 간결하게 줄일 수 있다.
728x90
    
    
  반응형
    
    
    
  '대딩코딩 > 웹개발 스터디' 카테고리의 다른 글
| [Node.js] 11. 패키지 매니저와 PM2 (1) | 2023.11.18 | 
|---|---|
| [Node.js] 10. 동기와 비동기 & callback (0) | 2023.11.18 | 
| [Node.js] 8. 글목록 출력 (1) | 2023.11.16 | 
| [Node.js] 7. 배열과 반복 (0) | 2023.11.16 | 
| [Node.js] 6. Not found, 홈페이지 구현 (0) | 2023.11.16 |