node.js CRUD


var http = require('http');
var fs = require('fs');
var url = require('url');
var qs = require('querystring');

function templateHTML(title,list,body,control){//읽어와서 리턴
    return `<!doctype html>
    <html>
    <head>
    <title>WEB1 - ${title}</title>
    <meta charset="utf-8">
    </head>
    <body>
    <h1><a href="/">WEB55</a></h1>
    ${list}
    ${control}
    ${body}
    </body>
    </html>`
}
function templateList(data){
    var list = '<ul>';
    var i = 0; 
    while(i<data.length){
        list = list +`<li><a href="/?id=${data[i]}">${data[i]}</a></li>`;
        i=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;

    console.log(url.parse(_url,true));
    
    if(pathName==='/'){
        if(queryData.id === undefined){
            fs.readdir('./data', (error,data)=>{
                var title ="welcmoe"
                var description = "Hello, Node.js";
                var list =templateList(data);
                
                var template = templateHTML(title,list,`<h2>${title}</h2>${description}`,
                    '<a href="/create">create</a>');
                response.writeHead(200);
                response.end(template);
            });
        }else{
            fs.readFile(`data/${queryData.id}`,'utf-8', (err,description)=>{
                fs.readdir('./data', (error,data)=>{
                    var list =templateList(data);
                    var title=queryData.id;
                    var template = templateHTML(title,list,`<h2>${title}</h2>${description}`,
                    `<a href="/create">create</a>
                    <a href="/update?id=${title}">update</a>
                    <form action="delete_process" method="post">
                        <input type="hidden" name="id" value="${title}"/>
                        <input type="submit" value="delete"/>
                    </form>`);
                    response.writeHead(200);
                    response.end(template);
                });
            });
        }
    }else if(pathName==="/create"){
        fs.readdir('./data', (error,data)=>{
            var title="Web-Create";
            var list =templateList(data);
            var template = templateHTML(title,list,`
            <form action="/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'){
        let body ='';
        console.log("ffff")
        request.on('data',data=>{
            body += data;
            console.log();
        });
        request.on('end',()=>{
            let post = qs.parse(body);
            let title = post.title;
            let description = post.description;
            fs.writeFile(`data/${title}`,description,'utf-8',(err)=>{//타이틀 없으면 새로 만들어 description 넣기
                response.writeHead(302,{
                    Location:`/?id=${title}`
                });
                response.end('sucess');
            })
        });
    }else if(pathName==='/update'){
        fs.readdir('./data',(error,data)=>{
            fs.readFile(`data/${queryData.id}`, 'utf-8', (err,description)=>{
                let title = queryData.id;
                let list = templateList(data);
                let template = templateHTML(title, list,
                    `<form action="/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>`,
                    ''
                );
                response.writeHead(200);
                response.end(template);
            });
        });
    }else if(pathName==='/update_process'){
        let body ='';
        request.on('data', data=>{
            body+=data;
        });
        request.on('end',()=>{
            let post=qs.parse(body);
            let id = post.id;
            let title = post.title;
            let description = post.description;
            fs.rename(`data/${id}`, `data/${title}`,err=>{ //파일명 수정
                fs.writeFile(`data/${title}`, description,'utf-8'//내용 다시쓰기
                ,err=>{
                    response.writeHead(302,{Location:`/?id=${title}`});
                    response.end();
                })
            })
        });
    }else if(pathName==='/delete_process'){
        let body ='';
        request.on('data', data=>{
            body+=data;
        });
        request.on('end',()=>{
            let post=qs.parse(body);
            let id = post.id;
            fs.unlink(`data/${id}`,err=>{ //파일 삭제
                response.writeHead(302, {Location:`/`});
                response.end();
            });
        });
        
    }else{
        response.writeHead(404);
        response.end('Not found');
    }
    
    
});
app.listen(3000);