๋ฐ์ํ
[์ถ์ฒ-Youtube ์ํ์ฝ๋ฉ]
https://www.youtube.com/watch?v=ibz2gaW-tyQ&list=PLuHgQVnccGMA9QQX5wqj6ThK7t2tsGxjm&index=30
main.js
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 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 = '<ul>';
var i = 0;
while(i < filelist.length){
list = list + `<li><a href="/?id=${filelist[i]}">${filelist[i]}</a></li>`;
i = i + 1;
}
list = list+'</ul>';
var template = `
<!doctype html>
<html>
<head>
<title>WEB1 - ${title}</title>
<meta charset="utf-8">
</head>
<body>
<h1><a href="/">WEB</a></h1>
${list}
<h2>${title}</h2>
<p>${description}</p>
</body>
</html>
`;
response.writeHead(200);
response.end(template);
})
} else {
fs.readdir('./data', function(error, filelist){
var title = 'Welcome';
var description = 'Hello, Node.js';
var list = '<ul>';
var i = 0;
while(i < filelist.length){
list = list + `<li><a href="/?id=${filelist[i]}">${filelist[i]}</a></li>`;
i = i + 1;
}
list = list+'</ul>';
fs.readFile(`data/${queryData.id}`, 'utf8', function(err, description){
var title = queryData.id;
var template = `
<!doctype html>
<html>
<head>
<title>WEB1 - ${title}</title>
<meta charset="utf-8">
</head>
<body>
<h1><a href="/">WEB</a></h1>
${list}
<h2>${title}</h2>
<p>${description}</p>
</body>
</html>
`;
response.writeHead(200);
response.end(template);
});
});
}
} else {
response.writeHead(404);
response.end('Not found');
}
});
app.listen(3000);
data/Nodejs
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
๋ฐ์ํ
'JavaScript > Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Node.js-์ํ์ฝ๋ฉ] JavaScript - ํจ์์ ์ ๋ ฅ (0) | 2021.01.28 |
---|---|
[Node.js-์ํ์ฝ๋ฉ] JavaScript - ํจ์์ ๊ธฐ๋ณธ ๋ฌธ๋ฒ (0) | 2021.01.28 |
[Node.js-์ํ์ฝ๋ฉ] Node.js์์ ํ์ผ๋ชฉ๋ก ์์๋ด๊ธฐ (0) | 2021.01.27 |
[Node.js-์ํ์ฝ๋ฉ] JavaScript - ๋ฐฐ์ด๊ณผ ๋ฐ๋ณต๋ฌธ (0) | 2021.01.27 |
[Node.js-์ํ์ฝ๋ฉ] JavaScript - ๋ฐฐ์ด (0) | 2021.01.27 |
๋๊ธ