* 해당 포스터는 생활코딩 강의를 정리한 내용입니다.
[ update를 클릭했을 때 보여주는 화면 만들기 ]
1. form 필요
2. read 기능 필요 : 수정하고자 하는 데이터를 미리 넣어놔야한다.
→ id 값이 있는 경우에 대한 처리를 copy
else if (pathname === '/update') {
fs.readdir('./data', function(error, filelist) {
fs.readFile(`data/${queryData.id}`, 'utf8', function(err,description){
var list = templateList(filelist);
var title = queryData.id
var template = templateHTML(title,list,`<h2>${title}</h2>
<p>${description}</p>`,`<a href="/create">create</a> <a href="/update?id=${title}">update</a>`);
response.writeHead(200);
response.end(template);
});
});
}
HTML input 태그는 lu라는 속성이 있다.
<input type="text" name="title" placeholder="title" value='${title}'> 에서 value='${title}'를 작성해 준다면
value의 값이 기본값으로 들어오게 된다.
textarea의 기본값은 textarea 태그 안쪽에다가 넣어주면됨.
사용자가 수정한 정보를 전송할 때 어떤 파일을 수정할 것인지를 알 수 있어야함
하지만 title로 전송한 정보를 활용하면 문제가 생김 → 사용자가 title을 수정할 수도 있기 때문이다.
그래서 <input type="text" name="id" value="${title}"> 기본값으로 생성
이때 ${title}을 사용자가 수정하면 안되고 화면에 보여봤자 의미가 없음 → type="hidden"을 사용
else if (pathname === '/update') {
fs.readdir('./data', function(error, filelist) {
fs.readFile(`data/${queryData.id}`, 'utf8', function(err,description){
var list = template.list(filelist);
var title = queryData.id
var html = template.HTML(title,list,
//forn에서 submit을 했을 때 사용자가 정보를 update_process로 보내게 변경
`<form action="http://localhost:3000/update_process" method="post">
<input type="hidden" name="id" value="${title}">
<p><input type="text" name="title" placeholder="title" value='${title}'></p>
<p>
<textarea name="description" placeholder="description">${description}</textarea>
</p>
<p>
<input type="submit">
</p>
</form>`,
`<a href="/create">create</a> <a href="/update?id=${title}">update</a>`
)
response.writeHead(200);
response.end(html);
});
});
}
submit을 누르면 hidden으로 되어있는 control의 id라는 이름으로 value 값이 전송되므로
update_process에서는 수정되지 않은 수정할 파일의 이름을 받을 수 있다.
var http = require('http');
var fs = require('fs');
var url = require('url');
function templateHTML(title,list,body, control){
return `
<!doctype html>
<html>
<head>
<title>WEB1 - ${title}</title>
<meta charset="utf-8">
</head>
<body>
<h1><a href="/">WEB</a></h1>
${list}
${control}
${body}
</body>
</html>
`
}
function templateList(filelist){
var list = '<ul>';
var i = 0;
while( i < filelist.length ){
list = list + `<li><a href="/?id=${filelist[i]}">${filelist[i]}</a></li>`
i += 1
}
list = list + '</ul>';
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;
var qs = require('querystring')
//qs는 querystring이라는 NodeJS가 가지고 있는 모듈을 가져온다.
if( pathname === '/'){
if( queryData.id === undefined ){ //home
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>
<p>${description}</p>`,`<a href="/create">create</a>`);
response.writeHead(200);
response.end(template);
})
} else { //id 페이지를 선택한 페이지
fs.readdir('./data', function(error, filelist) {
fs.readFile(`data/${queryData.id}`, 'utf8', function(err,description){
var list = templateList(filelist);
var title = queryData.id
var template = templateHTML(title,list,`<h2>${title}</h2>
<p>${description}</p>`,`<a href="/create">create</a> <a href="/update?id=${title}">update</a>`);
response.writeHead(200);
response.end(template);
});
});
}
}
else if( pathname === '/create' ){
fs.readdir('./data', function(error, filelist) {
var title = 'WEB-Create'
var list = templateList(filelist);
var template = templateHTML(title,list,`
<form action="http://localhost:3000/create_process" method="post">
<p><input type="text" name="title" placeholder="title"></p>
<p>
<textarea name="description" placeholder="description"></textarea>
</p>
<p>
<input type="submit">
</p>
</form>
`,'');
response.writeHead(200);
response.end(template);
});
}
else if( pathname === '/create_process' ){
var body ='';
request.on('data',function(data){
body = body + data
});
request.on('end', function(){
var post = qs.parse(body);
var title = post.title;
var description = post.description;
fs.writeFile(`data/${title}`, description, 'utf8', function(err){
response.writeHead(302, {Location: `/?id=${title}`});
response.end();
} )
});
}
else if (pathname === '/update') {
fs.readdir('./data', function(error, filelist) {
fs.readFile(`data/${queryData.id}`, 'utf8', function(err,description){
var list = templateList(filelist);
var title = queryData.id
var template = templateHTML(title,list,
//forn에서 submit을 했을 때 사용자가 정보를 update_process로 보내게 변경
`<form action="http://localhost:3000/update_process" method="post">
<input type="hidden" name="id" value="${title}">
<p><input type="text" name="title" placeholder="title" value='${title}'></p>
<p>
<textarea name="description" placeholder="description">${description}</textarea>
</p>
<p>
<input type="submit">
</p>
</form>`,
`<a href="/create">create</a> <a href="/update?id=${title}">update</a>`
)
response.writeHead(200);
response.end(template);
});
});
}
else {
response.writeHead(404);
response.end('Not Found');
}
});
app.listen(3000);
'Back-End > App' 카테고리의 다른 글
[ NodeJS - APP ] 글 삭제 버튼 구현 (0) | 2021.08.12 |
---|---|
[ NodeJS - APP ] 수정된 내용 저장 (0) | 2021.08.12 |
[ NodeJS - APP ] 글 수정 링크 생성 (0) | 2021.08.12 |
[ NodeJS - APP ] 파일 생성과 리다이렉션 (0) | 2021.08.12 |
[ NodeJS - APP ] post 방식으로 전송된 데이터 받기 (0) | 2021.08.12 |