Share
Sign In

Ch.8 본문을 다른 디렉토리의 파일로 옮기기 - Node.js

디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.
파일을 만들고 본문을 옮겼다면 main.js에서 본문내용을 지워버리자.
그리고 파일을 읽는 fs모듈의 readFile() 함수를 사용하여 본문내용을 불러오도록 수정해보자.
우선 공식문서의 readFile()에 들어가야할 argument값을 모두 살펴보자.
공식문서를 살펴보면 fileLocation, option, callback 3가지가 포함되어야 한다.
우리는 파일위치를 url의 /?id=""의 값으로 받을것이므로 `(grave accent)를 이용하자.
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'){ return response.writeHead(404); } response.writeHead(200); // readFile()함수를 사용 fs.readFile(`data/${queryData.id}`,'utf8',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> <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> ${description} <!-- 템플릿 리터럴 사용--> </p> </body> </html> `; response.end(template); }); }); app.listen(3000);
Node.js 홈으로 돌아가기
Node.js / Express.js
메인으로 돌아가기
디렉토리 생성
다른 디렉토리에 파일의 본문을 저장하고, Node.js의 파일 읽기 기능(fs.readFile)을 이용해서 본문을 생성하는 방법을 살펴보자.
우선 root 폴더에 data라는 디렉토리를 생성하자. 이 디렉토리에 본문 내용이 들어있는 파일이 들어갈 것이다. 이때 html, css, javascript 문서 내용이 모두 다르므로 3개의 파일을 만들자.
그리고 html.1의 본문을 html파일에 본문의 내용을 넣으면 된다. 이때 본문의 내용은 <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.